Search code examples
javajcheckbox

How to uncheck(untick) the jCheckBox automatically when I close its child frame?


I am working on a Java application. The main class frame name as "a". In frame "a", there is one component - jCheckBox. So when I check(tick) this jCheckBox, it open another frame "b". I wanted to untick the jCheckBox when I close frame "b", but it seems like cannot works. Any idea how to solve this? Thanks.

Edit: However, I could close frame "b" by untick the jCheckBox in frame a (in main class frame).What I want to achieve is when I close frame "b", it should automatically uncheck the jCheckBox in frame "a". IDE show me a lot of errors after I compile my apps.

My code: (In Main frame A)

  private void jCheckBoxInfoActionPerformed(java.awt.event.ActionEvent evt) {                                              
       if (jCheckBoxInfo.isSelected()) {
                System.err.println("Frame B is opened");
                b.setVisible(true);
            } else {
                System.err.println("Frame B is closed");
                frameInfo.setVisible(false);
            }
        }

In frame B:

private void formWindowClosing(java.awt.event.WindowEvent evt) {   
   boolean selected = a.jCheckBoxInfo.isSelected();
   System.err.println(selected); //To check the status of jCheckBoxInfo
   a.jCheckBoxInfo.setSelected(false);  }

Output:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Win32GraphicsConfig.java:222)
    at java.awt.Window.init(Window.java:505)
    at java.awt.Window.<init>(Window.java:537)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:189)
    at b.<init>(b.java:30)
    at a.<init>(a.java:36)
    at b.<init>(b.java:26)
    at a.<init>(a.java:36)

Solution

  • In frame A, I did make a public access to class b. Code --> public b frameInfo = new b(); While in frame B, I did make the similar call back to class a. Code --> public a frameMain = new a(); I am trying to make the application automatically uncheck jCheckBoxInfo in frame a.

    That new a() is the problem; you're not unchecking the box in the existing frame, instead you're creating a new window and unchecking it there (which in turn results in more windows being created ad infinity; or rather until the stack overflow).

    Instead you can:

    1. Add the WindowListener while in a (probably using an anonymous internal class, extending WindowAdapter), instead of in b. Then you can directly refer the checkbox in windowClosing(). This is likely the cleanest approach as it avoids binding b more tightly to a than necesary, and keeps the visibility handling completely in a.
    2. If the b frame is a dialog with a as the parent window, you can get the owner window with: getOwner(), cast it to a and untick the check box there.
    3. If there is no owner window (in which case see about using multiple frames), you can pass the a instance to b, for example as a constructor parameter:

      class b {
          final a parent;
          public b(a parent) {
              this.parent = parent;
              ...
          }
      }
      

      and then you can just uncheck the checkbox using the parent reference.

    4. If b is an internal class of a, it has an implicit reference to the enclosing object, and it can be referred as a.this.

    In any case, use a reference to the existing a, where from you created the b, instead of creating a new a in b.