-2

I have the following situation. There is a class JFrame1 that executes a serie of instructions and gives focus to another JFrame2 and then resumes. When JFrame2 has got focus, JFrame1 should expect that JFrame2 disposes rather than perform the following instructions. What to do?

JFrame1{
   //instruction 1
   //instruction 2
   // ....
   //instruction 6
   JFrame2 starts
   [waiting for results of JFrame2]
   //instruction 7
   //instruction 8}

JFrame1 should execute instructions 7 and 8 when resumes focus

user
  • 245
  • 1
  • 5
  • 13
  • Swing is single-threaded, you can't use threads for this. Your model of thinking about this is all wrong, different windows don't really perform actions in parallel, they dispatch events which are serially processed. Use events to communicate between GUI components. – millimoose Feb 18 '13 at 17:15
  • 1
    You're asking the [same damn question](http://stackoverflow.com/questions/14936661/build-a-progress-bar-without-launching-a-new-thread/14937166#14937166") over and over again. Please knock this off. Ask for clarification in *one* question and *one* question only. This is not fair to the volunteers who support this site. – Hovercraft Full Of Eels Feb 18 '13 at 17:18
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 18 '13 at 22:03

4 Answers4

3

Normally it is not good to have a JFrame start another JFrame unless they are completely independent. I reccomend that you launch a JDialog instead of JFrame2. A JDialog is used the same way as a JFrame and all the commands are the same. The JFrame that launches the JDialog will pause until the JDialog is disposed with this.dispose() being called from within the JDialog class. Then it would look like this:

JFrame1 starts
instruction 1
instruction 2
...
instruction 6
JDialog (like JFrame2) starts
(JFrame1 waiting for JDialog to be disposed with this.dispose())
instruction 7
...
0

You could have a look at http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#isFocusOwner()

HTH

Fafhrd
  • 436
  • 2
  • 6
0

I assume that you mean that currently, instructions 7 and 8 are happening immediately after frame 2 opens. To execute instructions 7 and 8 after JFrame2 is closed, you can either make JFrame2 a modal dialog with JDialog or JOptionPane, or you can attach a listener to JFrame2 as follows:

frame2.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        // instruction 7
        // instruction 8
    }
});
Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
0

You should try to organize what you’re doing into one class and create objects. Obviously this is an extremely simplified example:

public class Frames extends JFrame{
  public Frames(){
   super("Frame 1");
   setSize(500,500);
   //setup frame 1 look
   doWorkMethodForFrame1();
  }
  public Frames(int arg){
   super("Frame 2");
   setSize(500,500);
   //setup frame 2 look
    doWorkMethodForFrame2(data);
  }


   public void doWorkMethodForFrame1(){
   //do work
   setVisible(false);
   }  
   public void doWorkMethodForFrame2(int data){
   //do work
   setVisible(false);
   }




}

//call your frames
new Frames();
new Frames(dataSend);
Petro
  • 3,484
  • 3
  • 32
  • 59