Skip to main content

APIzation: Replication Package

[Q#4286759][A#4286828] How to show/hide JPanels in a JFrame?

https://stackoverflow.com/q/4286759

The application I am developing is a game. What I want to do is have JPanels that appear in the JFrame, like a text or message window, and then disappear when they are no longer used. I have designed these JPanels in Netbeans as external classes and want to be able to call them in an actionPerformed() method. JOptionPanes or other popup dialogs are not an option because they take the focus away from the game. I also saw someone suggest a CardLayout in a similar question. This is not what I want because I am not just trying to swap the panes. They should go away when the program tells them to. How would I do this, say by binding it to a JButton Action?

Answer

https://stackoverflow.com/a/4286828

You can hide a JPanel by calling setVisible(false). For example:

APIzation

public static void main(String args[]){
    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    final JPanel p = new JPanel();
    p.add(new JLabel("A Panel"));
    f.add(p, BorderLayout.CENTER);

    //create a button which will hide the panel when clicked.
    JButton b = new JButton("HIDE");
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
                p.setVisible(false);
        }
    });

    f.add(b,BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
}
package com.stackoverflow.api;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Human4286828 {

  public static void hideJPanel() {
    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    final JPanel p = new JPanel();
    p.add(new JLabel("A Panel"));
    f.add(p, BorderLayout.CENTER);

    //create a button which will hide the panel when clicked.
    JButton b = new JButton("HIDE");
    b.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          p.setVisible(false);
        }
      }
    );

    f.add(b, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
  }
}

package com.stackoverflow.api;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * How to show/hide JPanels in a JFrame?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/4286828">https://stackoverflow.com/a/4286828</a>
 */
public class APIzator4286828 {

  public static void show() {
    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    final JPanel p = new JPanel();
    p.add(new JLabel("A Panel"));
    f.add(p, BorderLayout.CENTER);
    // create a button which will hide the panel when clicked.
    JButton b = new JButton("HIDE");
    b.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          p.setVisible(false);
        }
      }
    );
    f.add(b, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
  }
}