Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

java - EDT location with several JFrame window

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    });

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...