Event Dispatcher Thread

In the tutorial Java Event Dispatcher thread in Graphics, we'll illustrate you how to handle events in graphics using Java.

Event Dispatcher Thread

In the tutorial Java Event Dispatcher thread in Graphics, we'll illustrate you how to handle events in graphics using Java.

Event Dispatcher Thread

Event Dispatcher Thread

     

In the tutorial Java Event Dispatcher thread in Graphics, we'll illustrate you how to handle events in graphics using Java.

This example is going to show you Event Dispatcher thread that will wait for the events to occur and then dispatches those events to the appropriate event handlers.

In the given example, On selecting the color through buttons, the ActionListener class is called and different shades of the selected color will appears. By using Runnable interface in the constructor of Thread class, a thread has been started and has not yet been stopped. A stream of pseudorandom numbers is generated by the class Random. The class TableCellRenderer renders the cells in a JTable and the method currentTimeMillis() will returns the current time in milliseconds.

Following code selects a random sequence determined by the current time:

Random random = new Random(System.currentTimeMillis());

The method isEventDispatchThread() of class SwingUtilities returns true if the current is an AWT event dispatching thread. The method fireTableCellUpdated (row, column) notifies all the listeners that the value of the cell has been updated. The method invokeLater() of class SwingUtilities is used to update the GUI. In the following example the invokeLater() calls the constructor of Runnable interface.Thread.sleep(600) method blocks the current thread for the specified number of milliseconds. After every 600 milliseconds, the color appears with different shades.

Here is the code of EventDispatcherThread.java

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), 00);
  else if (type == blue) {
  color = new Color(00, 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(102102));
  }
 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);
  }
}

If you select the color red, output will be:

If you select the color blue, output will be:

Download Source Code