0

I have this 2 JFrame classes:

   public class Frame1 extends javax.swing.JFrame {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      private JButton button1;
      button1 = new JButton("Open Frame2");
      private void button1ActionPerformed(java.awt.event.ActionEvent evt) {    
      this.setEnabled(false);  // disable Frame1 until Frame2 is showing              
      java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame2 obj2 = new Frame2 ();
                obj2.setVisible(true); 
            }
        });
      }
    }


 public class Frame2 extends javax.swing.JFrame {
  setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
      public Frame2 () {
      this.setVisible(false);
      }
   Frame1 obj1 = new Frame1 ();
   private JButton button2;
      button2 = new JButton("Hide Frame2 and go to Frame1");
      private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
           this.setVisible(false);
      }
    }

As you can see, when I click on button1 Frame1 gets disabled and Frame2 obj2 is created.

  1. First of all I'd to know whether what I have done is a correct way to disable/defocus one parent JFrame to show another on top with focus.
  2. And then I just need to enable Frame1 after hiding Frame2, something like obj1.setEnabled(true). How can I do that?
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frank
  • 2,083
  • 8
  • 34
  • 52
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) (Make one frame a modal dialog, or just use a `CardLayout`). – Andrew Thompson Jul 14 '13 at 12:45
  • ok thanks, I change Frame2 to "public class Dialog2 extends javax.swing.JDialog {}" now how can I reach my aim? And how can I make Dialog2 child of Frame1 parent? – Frank Jul 14 '13 at 14:37
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that reflects your understanding of [*How to Make Dialogs*](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). – trashgod Jul 14 '13 at 15:12

1 Answers1

0

All what you have to do is to create an ref of your first class frame right inside it:

public 1_class_name obj;
//call the second class method getobj(o)
getobj(obj);

and then create a method and a ref in the second class frame:

1_class_name a;
public void getobj(1_class_name o){
    a=o;
}

After you do that, you create a listener in the second class frame:

        public void windowClosing(WindowEvent e) {
            a.setEnabled(true);
            dispose();
        }
    };
    addWindowListener(exitListener);

It worked for me, I hope it will work for you too ;)