1.JFrame call 2. JFrame - wait() =>both blocked?

553 views Asked by At

I've got a problem: I've got a jframe1 who calls at ActionPerformed the jframe2. JFrames are threads or? and so I've tried in the jframe2 the wait() method, and then I would notify the jframe2's in jframe1..

my code in jframe2 (a method what run, when a button is clicked):

private void read(){

    synchronized(jframe1){
        try {

            if(writer.checkLast() == null){
                this.wait();
                jLabel.setText(writer.getLast());
            }
            else{
                jLabel.setText(writer.getLast());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

But the Problem is, that if I use this.wait(); in jframe2, my jframe1 is also locked.. what I do wrong?

sry for my bad english, thanks if anyone have got an answer!

2

There are 2 answers

0
Hovercraft Full Of Eels On

I get the feeling that you're trying to emulate the behavior of a modal dialog by using the wait() method, but as Michael explains well above, don't call wait on a Swing component and don't use Thread.sleep. Instead if you want to display another window modally use a JOptionPane or a modal JDialog. It's all well explained in the tutorials.

0
Michael Borgwardt On

Frames are threads or?

No, absolutely not. There is one single thread in which all painting and user input events happen, the Event Dispatch Thread. However, this thread is different from the application's main thread, which is probably what lead you to believe that each frame has its own thread.

Since all events happen on the event dispatch thread, you don't have to do any synchronization, and your frames can call each other's methods without requiring any synchronization or notification. Which is the reason for the single threaded design in the first place (The general consensus is that a multithreaded GUI is nearly impossible to work with).