Search code examples
javaswingjframeactionjbutton

The setVisible(false) function for a JFrame does not make the JFrame disappear


I'm trying to make a button on a JFrame cause that JFrame to disappear and call another JFrame. It should reappear in the same state later when the new JFrame is dismissed. I can call the new JFrame successfully, but the setVisible(false) function appears to do nothing.

Here's the code for the JFrame I want to make disappear temporarily.

First the Main class:

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run () {
                JFrame NavigationGUITest = new JFrame();
                NavigationGUITest.setContentPane(new NavigationGUITest().NavigationButtonsPanel);
                NavigationGUITest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                NavigationGUITest.pack();
                NavigationGUITest.setSize(300,400);
                NavigationGUITest.setVisible(true);
            }
        });
    }
}

Then the JFrame class for the Frame I want to make disappear (but that refuses to disappear):

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run () {
                JFrame NavigationGUITest = new JFrame();
                NavigationGUITest.setContentPane(new NavigationGUITest().NavigationButtonsPanel);
                NavigationGUITest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                NavigationGUITest.pack();
                NavigationGUITest.setSize(300,400);
                NavigationGUITest.setVisible(true);
            }
        });
    }
}

I've used a different syntax for each of the action listeners to illustrate how my problem turns up in both cases.

Finally, here's my navigationGUITest.form:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="NavigationGUITest">
  <grid id="27dc6" binding="NavigationButtonsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="0" left="0" bottom="0" right="0"/>
    <constraints>
      <xy x="20" y="20" width="500" height="400"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <component id="9b808" class="javax.swing.JButton" binding="CreateNewRecordBtn">
        <constraints>
          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Create a new record"/>
        </properties>
      </component>
      <component id="dbdac" class="javax.swing.JButton" binding="EditRecordBtn">
        <constraints>
          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Edit an existing record"/>
        </properties>
      </component>
    </children>
  </grid>
</form>

To recap. my question is: why doesn't setVisible(false) in the actionlistener clauses for the buttons cause the Navigation JFrame to disappear?

I'm perfectly sure I'm missing something pretty obvious, but for the life of me I can't identify what it is.


Solution

  • You're making this mistake a couple times.

    JFrame NavigationGUITest = new JFrame();
    NavigationGUITest.setContentPane(new NavigationGUITest().NavigationButtonsPanel);
    

    Why would you use setContentPane? Just try.

    NavigationGUITest navigationGUITest = new NavigationGUITest(); 
    

    Then if you must.

    navigationGUITest.setContentPane( navigationGUITest.NavigationButtonsPanel );