Advertisement
rutera

Is Event Dispatcher Thread

Oct 19th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Random;
  7. // by rutera
  8. import javax.swing.AbstractAction;
  9. import javax.swing.ButtonGroup;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JRadioButton;
  15. import javax.swing.JTable;
  16. import javax.swing.SwingUtilities;
  17. import javax.swing.table.AbstractTableModel;
  18. import javax.swing.table.TableCellRenderer;
  19.  
  20. public class IsEDTExample extends JPanel {
  21.   private boolean keepRunning;
  22.  
  23.   private static int RED = 0;
  24.  
  25.   private static int BLUE = 1;
  26.  
  27.   private static int GREEN = 2;
  28.  
  29.   private static int VARIABLE = 3;
  30.  
  31.   private static int SIZE = 3;
  32.  
  33.   private int threadShade;
  34.  
  35.   private ColorTableModel tableModel= new ColorTableModel();
  36.  
  37.   private Thread colorShadeThread;
  38.  
  39.   public IsEDTExample() {
  40.     JTable table = new JTable(tableModel);
  41.     table.setRowHeight(100);
  42.     table.setDefaultRenderer(Object.class, new ColorRenderer());
  43.     add(table);
  44.  
  45.     add(new JLabel("Thread Color Shade:"));
  46.     ButtonGroup group = new ButtonGroup();
  47.     JRadioButton redOption = new JRadioButton("Red");
  48.     group.add(redOption);
  49.     redOption.addActionListener(new ActionListener() {
  50.       public void actionPerformed(ActionEvent e) {
  51.         threadShade = RED;
  52.       }
  53.     });
  54.  
  55.     JRadioButton blueOption = new JRadioButton("Blue");
  56.     group.add(blueOption);
  57.     blueOption.addActionListener(new ActionListener() {
  58.       public void actionPerformed(ActionEvent e) {
  59.         threadShade = BLUE;
  60.       }
  61.     });
  62.  
  63.     JRadioButton greenOption = new JRadioButton("Green");
  64.     group.add(greenOption);
  65.     greenOption.addActionListener(new ActionListener() {
  66.       public void actionPerformed(ActionEvent e) {
  67.         threadShade = GREEN;
  68.       }
  69.     });
  70.  
  71.     redOption.setSelected(true);
  72.     this.threadShade = RED;
  73.  
  74.     add(redOption);
  75.     add(greenOption);
  76.     add(blueOption);
  77.  
  78.     add(new JButton(new RandomColorAction()));
  79.  
  80.     this.keepRunning = true;
  81.     this.colorShadeThread = new Thread(new RandomColorShadeRunnable());
  82.     this.colorShadeThread.start();
  83.   }
  84.  
  85.   private class RandomColorAction extends AbstractAction {
  86.     public RandomColorAction() {
  87.       super("Create Random Color");
  88.     }
  89.  
  90.     public void actionPerformed(ActionEvent e) {
  91.       IsEDTExample.this.tableModel.generateRandomColor(VARIABLE);
  92.     }
  93.   }
  94.  
  95.   private class ColorTableModel extends AbstractTableModel {
  96.     private Color[][] colors = new Color[3][3];
  97.  
  98.     public ColorTableModel() {
  99.       for (int i = 0; i < SIZE; i++) {
  100.         for (int x = 0; x < SIZE; x++) {
  101.           colors[i][x] = Color.white;
  102.         }
  103.       }
  104.     }
  105.  
  106.     public int getRowCount() {
  107.       return SIZE;
  108.     }
  109.  
  110.     public int getColumnCount() {
  111.       return SIZE;
  112.     }
  113.  
  114.     public Object getValueAt(int rowIndex, int columnIndex) {
  115.       return colors[rowIndex][columnIndex];
  116.     }
  117.  
  118.     public void generateRandomColor(int type) {
  119.       Random random = new Random(System.currentTimeMillis());
  120.       final int row = random.nextInt(SIZE);
  121.       final int column = random.nextInt(SIZE);
  122.       final Color color;
  123.       if (type == RED) {
  124.         color = new Color(random.nextInt(256), 0, 0);
  125.       } else if (type == BLUE) {
  126.         color = new Color(0, 0, random.nextInt(256));
  127.       } else if (type == GREEN) {
  128.         color = new Color(0, random.nextInt(256), 0);
  129.       } else {
  130.         color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
  131.       }
  132.  
  133.       if (SwingUtilities.isEventDispatchThread()) {
  134.         colors[row][column] = color;
  135.         fireTableCellUpdated(row, column);
  136.       } else {
  137.         SwingUtilities.invokeLater(new Runnable() {
  138.           public void run() {
  139.             colors[row][column] = color;
  140.             fireTableCellUpdated(row, column);
  141.           }
  142.         });
  143.       }
  144.     }
  145.   }
  146.  
  147.   private class ColorRenderer implements TableCellRenderer {
  148.     private JLabel label;
  149.  
  150.     public ColorRenderer() {
  151.       label = new JLabel();
  152.       label.setOpaque(true);
  153.       label.setPreferredSize(new Dimension(100, 100));
  154.     }
  155.  
  156.     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
  157.         boolean hasFocus, int row, int column) {
  158.       label.setBackground((Color) value);
  159.       return label;
  160.     }
  161.   }
  162.  
  163.   private class RandomColorShadeRunnable implements Runnable {
  164.     public void run() {
  165.       while (keepRunning) {
  166.         tableModel.generateRandomColor(threadShade);
  167.         try {
  168.           Thread.sleep(500);
  169.         } catch (InterruptedException e) {
  170.         }
  171.       }
  172.     }
  173.   }
  174.  
  175.   public static void main(String[] a) {
  176.     JFrame f = new JFrame("Is Event Dispatch Thread Example");
  177.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  178.     f.add(new IsEDTExample());
  179.     f.pack();
  180.     f.setVisible(true);
  181.   }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement