Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:16 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Thread of Event Dispatcher
PostPosted: Tue Nov 11, 2008 5:51 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Thread of Event Dispatcher
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.*;

public class EventDispatcherExample extends JPanel {
boolean running;
int red = 0;
int blue = 1;
int s = 2;
int thread;
ColorTableModel colorTableModel= new ColorTableModel();
Thread colorShadeThread;

  public EventDispatcherExample() {
    JTable table = new JTable(colorTableModel);
    table.setRowHeight(100);
    table.setDefaultRenderer(Object.class, new ColorRenderer());
    add(table);
    ButtonGroup value = new ButtonGroup();
    JButton button1 = new JButton("Red");
    value.add(button1);
    button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        thread = red;
      }
    });
    JButton button2 = new JButton("Blue");
    value.add(button2);
    button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        thread = blue;
      }
    });
    add(button1);
    add(button2);
   
    this.running = true;
    this.colorShadeThread = new Thread(new RandomColorShadeRunnable());
    this.colorShadeThread.start();
  }
  private class ColorTableModel extends AbstractTableModel {
    private Color[][] colors = new Color[3][3];

    public ColorTableModel() {
      for (int i = 0; i < s; i++) {
        for (int j = 0; j < s; j++) {
          colors[i][j] = Color.white;
        }
      }
    }
  public int getRowCount() {
      return s;
    }
  public int getColumnCount() {
      return s;
    }
  public Object getValueAt(int rowIndex, int columnIndex) {
      return colors[rowIndex][columnIndex];
    }
  public void generateRandomColor(int type) {
      Random random = new Random(System.currentTimeMillis());
      final int row = random.nextInt(s);
      final int column = random.nextInt(s);
      final Color color;
      if (type == red) {
        color = new Color(random.nextInt(200), 0, 0);
      } else if (type == blue) {
        color = new Color(0, 0, random.nextInt(200));
      } else {
        color = new Color(random.nextInt(200), random.nextInt(200),
            random.nextInt(200));
      }

      if (SwingUtilities.isEventDispatchThread()) {
        colors[row][column] = color;
        fireTableCellUpdated(row, column);
      } else {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            colors[row][column] = color;
            fireTableCellUpdated(row, column);
          }
        });
      }
    }
  }
   private class ColorRenderer implements TableCellRenderer {
    private JLabel label;

    public ColorRenderer() {
      label = new JLabel();
      label.setOpaque(true);
      label.setPreferredSize(new Dimension(102, 102));
    }
   public Component getTableCellRendererComponent(JTable table, Object
      value,boolean isSelected,boolean hasFocus, int row, int column) {
      label.setBackground((Color) value);
      return label;
    }
  }
  private class RandomColorShadeRunnable implements Runnable {
    public void run() {
      while (running) {
        colorTableModel.generateRandomColor(thread);
        try {
          Thread.sleep(600);
        } catch (Exception e) {}
      }
    }
  }
   public static void main(String[] args) {
    JFrame frame = new JFrame("Event Dispatch Example");
    frame.add(new EventDispatcherExample());
  frame.setSize(450,300);
    frame.setVisible(true);
  }
}


Attachments:
File comment: If you select the color red, output will be:
thread1.gif
thread1.gif [ 8.75 KiB | Viewed 1779 times ]
File comment: If you select the color blue, output will be:
thread2.gif
thread2.gif [ 8.49 KiB | Viewed 1785 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW
 Event Handling Notes
 How can I pass a PHP variable to a window.onload event funct
 daemon thread - What is the use of deamon thread?
 Multi Thread Program
 event results from the clicking of a button
 relationship an event-listener interface & event handler
 thread cannot acquire a lock on an object
 high-level thread states
 advantage of the event delegation model over event-inherit

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team