Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermining scope of Event Dispatch Thread
    primarykey
    data
    text
    <p>I am new to swing and still learning the ins and outs of it. I wrote a basic code and started experimenting with EDT. Here is the code:</p> <pre><code>public class SwingDemo2 extends Thread implements ActionListener { JLabel jl; SwingDemo2() { JFrame jfr = new JFrame("Swing Event Handling"); jfr.setSize(250, 100); jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jl = new JLabel("Press a button!", SwingConstants.CENTER); System.out.println("After Label: " + SwingUtilities.isEventDispatchThread()); JButton jb1 = new JButton("OK"); jb1.setActionCommand("OK"); jb1.addActionListener(this); JButton jb2 = new JButton("Reset"); jb2.setActionCommand("Reset"); jb2.addActionListener(this); jfr.add(jl, BorderLayout.NORTH); jfr.add(jb1, BorderLayout.WEST); jfr.add(jb2, BorderLayout.EAST); System.out.println("After adding: " + SwingUtilities.isEventDispatchThread()); jfr.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { System.out.println("In main: " + SwingUtilities.isEventDispatchThread()); new SwingDemo2(); } }); } public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand() == "OK") { System.out.println("In OK: " + SwingUtilities.isEventDispatchThread()); jl.setText("You pressed Ok"); } else if (ae.getActionCommand() == "Reset") { System.out.println("In Reset: " + SwingUtilities.isEventDispatchThread()); jl.setText("You pressed Reset"); } } } </code></pre> <p>I added a few <code>isEventDispatchThread()</code> methods to verify the thread I am in. Apart from the GUI the messages in console were:</p> <pre><code>In main: true After Label: true After adding: true In OK: true In Reset: true </code></pre> <p>It seems all the time I was in the EDT. My question is, after the <code>jfr.setVisible(true)</code> statement shouldn't the <code>SwingDemo2()</code> constructor return to <code>main()</code> and shouldn't that be the end of EDT?</p> <p>I waited for many seconds before I first pressed any button in the GUI so why is still my event handling being done in EDT? Shouldn't that have given the EDT enough time to terminate?</p> <p>Thanx in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload