Código fuente del programa EventDispatchThreadTest
Ver la entrada correspondiente en el siguiente enlace:
Java Swing: Event Dispatch Thread (EDT) - Parte 3
import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowStateListener; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class EventDispatchThreadTest { private JFrame myOne; private JFrame myTwo; private JButton myButtonTask; private JButton myButtonReset; private JButton myButtonNothing; private JLabel myLabel; private int myClickCount; private int myResetCount; public EventDispatchThreadTest() { super(); JFrame one= new JFrame("One"); one.setName( one.getTitle() ); one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); one.setSize(300, 400); one.setLocation(0, 0); JButton buttonTask= new JButton("Long-running task"); JButton buttonReset= new JButton("Reset click count"); JPanel onePanel= new JPanel(); onePanel.setOpaque(true); onePanel.setBackground(Color.BLUE); onePanel.add(buttonTask); onePanel.add(buttonReset); one.setContentPane(onePanel); WindowListener auxWindowListener= EventDispatchThreadTest.createWindowListener(); one.addWindowListener(auxWindowListener); WindowStateListener auxWindowStateListener= EventDispatchThreadTest.createWindowStateListener(); one.addWindowStateListener(auxWindowStateListener); MouseListener auxMouseListener= EventDispatchThreadTest.createMouseListener(); onePanel.addMouseListener(auxMouseListener); MouseMotionListener auxMouseMotionListener= EventDispatchThreadTest.createMouseMotionListener(); onePanel.addMouseMotionListener(auxMouseMotionListener); JFrame two= new JFrame("Two"); two.setName( two.getTitle() ); two.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); two.setSize(300, 400); two.setLocation(400, 0); JLabel label= new JLabel("Never pressed"); JButton buttonNothing= new JButton("Nothing"); JPanel twoPanel= new JPanel(); twoPanel.setOpaque(true); twoPanel.setBackground(Color.BLUE); twoPanel.add(label); twoPanel.add(buttonNothing); two.setContentPane(twoPanel); buttonTask.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //This text is never shown. EventDispatchThreadTest.this.myLabel.setText("Left button pressed. Task running..."); for (int i= 0; i < 99999; i++) { String auxString= String.format("Working... %s %s%n", i, new Date(System.currentTimeMillis())); if ((i % 2) == 0) { System.out.print(auxString); } } EventDispatchThreadTest.this.myClickCount++; EventDispatchThreadTest.this.labelAutoText(); System.out.format("isEventDispatchThread=%s%n", SwingUtilities.isEventDispatchThread()); } }); buttonReset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { EventDispatchThreadTest.this.myClickCount= 0; EventDispatchThreadTest.this.myResetCount++; EventDispatchThreadTest.this.labelAutoText(); } }); this.myOne= one; this.myTwo= two; this.myButtonTask= buttonTask; this.myButtonReset= buttonReset; this.myButtonNothing= buttonNothing; this.myLabel= label; this.myClickCount= 0; this.myResetCount= 0; this.myOne.setVisible(true); this.myTwo.setVisible(true); } private void labelAutoText() { this.myLabel.setText(String.format("Click count: %s. Reset count= %s" , this.myClickCount , this.myResetCount )); } private static WindowListener createWindowListener() { WindowListener auxWindowListener= new WindowListener() { @Override public void windowOpened(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowClosing(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowClosed(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowIconified(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowDeiconified(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowActivated(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void windowDeactivated(WindowEvent e) { System.out.format("WindowEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } }; return auxWindowListener; } private static WindowStateListener createWindowStateListener() { WindowStateListener auxWindowStateListener= new WindowStateListener() { @Override public void windowStateChanged(WindowEvent e) { System.out.format("Window[State]Event (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } }; return auxWindowStateListener; } private static MouseListener createMouseListener() { MouseListener auxMouseListener= new MouseListener() { @Override public void mouseClicked(MouseEvent e) { System.out.format("MouseEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void mousePressed(MouseEvent e) { System.out.format("MouseEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void mouseReleased(MouseEvent e) { System.out.format("MouseEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void mouseEntered(MouseEvent e) { System.out.format("MouseEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void mouseExited(MouseEvent e) { System.out.format("MouseEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } }; return auxMouseListener; } private static MouseMotionListener createMouseMotionListener() { MouseMotionListener auxMouseMotionListener= new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { System.out.format("MouseMotionEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } @Override public void mouseMoved(MouseEvent e) { System.out.format("MouseMotionEvent (EDT? %s): %s%n", SwingUtilities.isEventDispatchThread(), e); } }; return auxMouseMotionListener; } public static void main(String[] args) { System.out.format("isEventDispatchThread=%s%n", SwingUtilities.isEventDispatchThread()); //GUI code should be executed in the EDT. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.out.format("isEventDispatchThread=%s%n", SwingUtilities.isEventDispatchThread()); new EventDispatchThreadTest(); } }); } }
No hay comentarios:
Publicar un comentario