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
If you select the color red, output will be:
If you select the color blue, output will be:
Liked it! Share this Tutorial