-1

I have been using a JButton in Netbeans to open a child JFrame in Java. I want the original parent frame not to dispose but to be disabled behind the child frame. So the code for the button action (to open the child frame) performed is:

this.setEnabled(false);
new Child().setVisible(true);

OK then everything is fine. I mean the child frame opens, but the problem is when I try to close it. I want the child frame to be invisible (not disabled) but the original parent frame to be enabled again. I have already set the default close operation to DO_NOTHING_ON_CLOSE, and I tried to use this:

this.dispose();
Parent.setEnabled(true);

However, this apparently does not work. I have read that I need to do some operations in the main class or I should use a dialog instead of another child frame but I am not familiar with them. Could you please suggest which solution would be more efficient for my work in Netbeans and also give some guidelines?

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • 3
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) *"I want the original parent frame not to dispose but to be disabled behind the child frame."* Use a modal `JDialog` instead of the 2nd frame. Here is an [example](https://stackoverflow.com/a/48035812/418556). – Andrew Thompson Jan 05 '18 at 21:53
  • Similar example on the MDI with JDesktopPane and JInternalFrame, as well as you cam replace the JDesktopPane with JFrame as of your requirement... – ArifMustafa Jan 06 '18 at 05:22

1 Answers1

-1

there is some deferent solution but I use this

  1. add parameter to Child constractor
  2. get Parent to Child class when you create new instance of it

child class

public class Child extends javax.swing.JFrame {

    JFrame parent;
    public Child(JFrame parent) {
        initComponents(); 
        this.parent = parent;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        dispose();
        parent.setEnabled(true);
    }

parent class button

private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.setEnabled(false);
    new Child(this).setVisible(true);
}
sadeghpro
  • 404
  • 2
  • 7
  • 22