EDT layout with multiple JFrame windows

I have a Swing JFrame. If I create a new JFrame in a new thread at runtime, where will the EDT be? In the current thread of the last JFrame or in the first window.

EDIT: Thanks for your answers.

I understand them, and I'm fine with them. I know that we should not create a swing object elsewhere that EDT, but I encounter problems.

I will explain; I developed a JAVA application for creating and extracting an archive such as winrar. You can create multiple archives at the same time with multiple threads. And recently, I wanted to add the status of information when creating an archive as a JprogressBar in a new JFrame with each creation. But my problem is to create a connection in a new state frame and the stream that the archive creates. Therefore, I create a JFrame in the archive stream to update the current progress bar.

But, as I could read it in various sources of information and your answers / comments, this is against java swing and performance; I cannot create a rocking object elsewhere that EDT.

But then, how should I solve my problem?

+3
java swing
Aug 26 '11 at 13:41
source share
3 answers

The EDT, the event dispatch stream, is separate from any particular GUI component, such as a JFrame.

As a rule, you should create all GUI components on EDT, but this does not mean that they own EDT, and EDT does not belong to these components.

To create two JFrames, both on EDT, you can do the following:

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame1 = new JFrame("Frame 1"); frame1.getContentPane().add(new JLabel("Hello in frame 1")); frame1.pack(); frame1.setLocation(100, 100); frame1.setVisible(true); } }); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame2 = new JFrame("Frame 2"); frame2.getContentPane().add(new JLabel("Hello in frame 2")); frame2.pack(); frame2.setLocation(200, 200); frame2.setVisible(true); } }); } 
+7
Aug 26 '11 at 13:44
source share

Event stream sent. It is not reassigned just because you created the Swing object in another thread (which should never do this anyway).

+5
Aug 26 '11 at 13:45
source share

it should be very simple, if all events executed in the current EDT, then the EDT does not exist, you can specify another queue,

  • from Swing Listeners,
  • according to the calendar enclosed in invokeLater / invokeAndWait ,
  • but the safest (and best for me) would be javax.swing.Action

Exit

 run: Time at : 19:35:21 There isn't Live EventQueue.isDispatchThread, why any reason for that There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that Time at : 19:35:21 Calling from EventQueue.isDispatchThread Calling from SwingUtilities.isEventDispatchThread Time at : 19:35:21 Calling from EventQueue.isDispatchThread Calling from SwingUtilities.isEventDispatchThread Time at : 19:35:51 There isn't Live EventQueue.isDispatchThread, why any reason for that There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that Time at : 19:36:21 There isn't Live EventQueue.isDispatchThread, why any reason for that There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that Time at : 19:36:51 There isn't Live EventQueue.isDispatchThread, why any reason for that There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that Time at : 19:37:21 There isn't Live EventQueue.isDispatchThread, why any reason for that There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that BUILD SUCCESSFUL (total time: 2 minutes 17 seconds) 

from code:

 import java.awt.EventQueue; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.*; import javax.swing.*; public class IsThereEDT { private ScheduledExecutorService scheduler; private AccurateScheduledRunnable periodic; private ScheduledFuture<?> periodicMonitor; private int taskPeriod = 30; private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); private Date dateRun; public IsThereEDT() { scheduler = Executors.newSingleThreadScheduledExecutor(); periodic = new AccurateScheduledRunnable() { private final int ALLOWED_TARDINESS = 200; private int countRun = 0; private int countCalled = 0; @Override public void run() { countCalled++; if (this.getExecutionTime() < ALLOWED_TARDINESS) { countRun++; isThereReallyEDT(); // non on EDT } } }; periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS); periodic.setThreadMonitor(periodicMonitor); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { isThereReallyEDT(); JFrame frame1 = new JFrame("Frame 1"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.getContentPane().add(new JLabel("Hello in frame 1")); frame1.pack(); frame1.setLocation(100, 100); frame1.setVisible(true); } }); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame2 = new JFrame("Frame 2"); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.getContentPane().add(new JLabel("Hello in frame 2")); frame2.pack(); frame2.setLocation(200, 200); frame2.setVisible(true); isThereReallyEDT(); } }); } private void isThereReallyEDT() { dateRun = new java.util.Date(); System.out.println(" Time at : " + sdf.format(dateRun)); if (EventQueue.isDispatchThread()) { System.out.println("Calling from EventQueue.isDispatchThread"); } else { System.out.println("There isn't Live EventQueue.isDispatchThread, why any reason for that "); } if (SwingUtilities.isEventDispatchThread()) { System.out.println("Calling from SwingUtilities.isEventDispatchThread"); } else { System.out.println("There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that "); } System.out.println(); } public static void main(String[] args) { IsThereEDT isdt = new IsThereEDT(); } } abstract class AccurateScheduledRunnable implements Runnable { private ScheduledFuture<?> thisThreadsMonitor; public void setThreadMonitor(ScheduledFuture<?> monitor) { this.thisThreadsMonitor = monitor; } protected long getExecutionTime() { long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS); return delay; } } 
+1
Aug 26 '11 at 17:39
source share



All Articles