EDT location with several JFrame window
Asked Answered
H

3

3

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

EDIT: Thanks for your answers.

I understand them and i'm ok with them. I know that we don't must create swing object elsewhere that the EDT but I meet problem.

I explain; I developed a JAVA application for create and extract archive like winrar. You can create several arhive in same time with multi-thread. And recently, I wanted add an information status during the archive creation in the form of JprogressBar in a new JFrame at every creation. But my problem is for generate a communication in the new status frame and the thread who create archive. That's why, I create the JFrame in the archive thread for update the progress bar currently.

But like i could read it in divers information source and on your answers/comments, it's against java swing and performance; I can't create swing object elsewhere that the EDT.

But then, how should I solve my problem ?

Homo answered 26/8, 2011 at 13:41 Comment(2)
You shouldn't create any Swing objects outside the EDT, so your question is based on an invalid premise. ;-)Oboe
It seems as though one of those JFrame instances should be a modal JDialog. The input to the frame will be blocked while the dialog is visible.Custodian
W
7

The EDT - the event dispatch thread - is separate from any concrete GUI component, such as a JFrame.

Generally you should create all GUI components on the EDT, but that does not mean they own the EDT, nor does the EDT own the components.

To create two JFrames, both on the 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);
        }
    });

}
Wenger answered 26/8, 2011 at 13:44 Comment(0)
O
5

The Event Dispatch Thread is fixed. It doesn't get reassigned just because you created a Swing object on another thread (which you should never do anyway).

Oboe answered 26/8, 2011 at 13:45 Comment(1)
The system always posts to the current event queue, even if it's restarted or replaced.Gastronome
F
1

that should be very simple, if all events all done in current EDT, then EDT doesn't exists, another Queue is possible to stating,

  • from Swing's Listeners,
  • by caling code wrapped into invokeLater / invokeAndWait,
  • but safiest (and best for me) would be javax.swing.Action

output

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;
    }
}
Fango answered 26/8, 2011 at 17:39 Comment(3)
+1 Even after add(new JLabel(String.valueOf(1/0))), the EDT lives!Gastronome
don't understand what your code is meant to demonstrate ... behaves exactly as I would expect: if isThereReallyEDT is called from EDT (as it is from the runnable of invokeLater), isEventDispatchThread is true, if called from off EDT (as it is in the scheduler's runnable) it is true. Not being an expert of threading, but pretty sure the edt is very much alive at all times, even if event queue might be empty occasionally ;-)Bradawl
@jfpoilpret can you please comment what's really happends with AWT-EventQueue, is possible to create secondFango

© 2022 - 2024 — McMap. All rights reserved.