How to show/hide JPanels in a JFrame?












16















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?










share|improve this question























  • +1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

    – SyntaxT3rr0r
    Nov 26 '10 at 16:03













  • Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

    – aharlow
    Nov 26 '10 at 16:06
















16















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?










share|improve this question























  • +1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

    – SyntaxT3rr0r
    Nov 26 '10 at 16:03













  • Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

    – aharlow
    Nov 26 '10 at 16:06














16












16








16


2






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?










share|improve this question














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?







java swing hide jframe jpanel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '10 at 15:59









aharlowaharlow

122129




122129













  • +1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

    – SyntaxT3rr0r
    Nov 26 '10 at 16:03













  • Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

    – aharlow
    Nov 26 '10 at 16:06



















  • +1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

    – SyntaxT3rr0r
    Nov 26 '10 at 16:03













  • Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

    – aharlow
    Nov 26 '10 at 16:06

















+1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

– SyntaxT3rr0r
Nov 26 '10 at 16:03







+1 to your question... Games very often have their own custom UI. There aren't many games out there that use any of the Java look'n'feel, at least not good looking ones. What kind of game are you making? which platform(s) are you targetting? (obviously not the iPhone/iPad) [Disclaimer: I used to work professionally in the video game industry].

– SyntaxT3rr0r
Nov 26 '10 at 16:03















Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

– aharlow
Nov 26 '10 at 16:06





Thanks! This is a very simple game, it's a first person RPG that uses static images as scenes. I am also hoping to figure out how to load multiple images on to the screen. Maybe this question will answer that too.

– aharlow
Nov 26 '10 at 16:06












4 Answers
4






active

oldest

votes


















23














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



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);
}





share|improve this answer
























  • I guess it was that simple!

    – aharlow
    Nov 27 '10 at 3:15











  • There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

    – bestyasser
    Apr 4 '17 at 15:06



















1














/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Style1.java
*
* Created on May 5, 2011, 6:31:16 AM
*/
package Test;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
*
* @author Sameera
*/
public class Style2 extends javax.swing.JFrame {

/** Creates new form Style1 */
public Style2() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
cmd_SH = new javax.swing.JButton();
pnl_2 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

cmd_SH.setText("Hide");
cmd_SH.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmd_SHActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(558, Short.MAX_VALUE)
.addComponent(cmd_SH)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(236, Short.MAX_VALUE)
.addComponent(cmd_SH)
.addContainerGap())
);

pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2);
pnl_2.setLayout(pnl_2Layout);
pnl_2Layout.setHorizontalGroup(
pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 621, Short.MAX_VALUE)
);
pnl_2Layout.setVerticalGroup(
pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 270, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(17, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {


System.out.println(evt.getActionCommand());
if (evt.getActionCommand().equals("Hide")) {
pnl_2.setVisible(false);
cmd_SH.setText("Show");
this.setSize(643, 294);
this.pack();


}
if (evt.getActionCommand().equals("Show")) {
pnl_2.setVisible(true);
cmd_SH.setText("Hide");
this.setSize(643, 583);
this.pack();
}
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Style1().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton cmd_SH;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel pnl_2;
// End of variables declaration
}





share|improve this answer





















  • 5





    Please don't just provide code-only answers.

    – Lee Taylor
    Nov 22 '12 at 23:52



















0














Call parent.remove(panel), where parent is the container that you want the frame in and panel is the panel you want to add.






share|improve this answer































    0














    If you want to hide panel on button click, write below code in JButton Action.
    I assume you want to hide jpanel1.



    jpanel1.setVisible(false);





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4286759%2fhow-to-show-hide-jpanels-in-a-jframe%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      23














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



      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);
      }





      share|improve this answer
























      • I guess it was that simple!

        – aharlow
        Nov 27 '10 at 3:15











      • There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

        – bestyasser
        Apr 4 '17 at 15:06
















      23














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



      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);
      }





      share|improve this answer
























      • I guess it was that simple!

        – aharlow
        Nov 27 '10 at 3:15











      • There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

        – bestyasser
        Apr 4 '17 at 15:06














      23












      23








      23







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



      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);
      }





      share|improve this answer













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



      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);
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 26 '10 at 16:08









      dogbanedogbane

      193k66320374




      193k66320374













      • I guess it was that simple!

        – aharlow
        Nov 27 '10 at 3:15











      • There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

        – bestyasser
        Apr 4 '17 at 15:06



















      • I guess it was that simple!

        – aharlow
        Nov 27 '10 at 3:15











      • There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

        – bestyasser
        Apr 4 '17 at 15:06

















      I guess it was that simple!

      – aharlow
      Nov 27 '10 at 3:15





      I guess it was that simple!

      – aharlow
      Nov 27 '10 at 3:15













      There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

      – bestyasser
      Apr 4 '17 at 15:06





      There is only one issue, change p.setVisible(false); by f.setVisible(false); to hide Panel.

      – bestyasser
      Apr 4 '17 at 15:06













      1














      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      /*
      * Style1.java
      *
      * Created on May 5, 2011, 6:31:16 AM
      */
      package Test;

      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JOptionPane;

      /**
      *
      * @author Sameera
      */
      public class Style2 extends javax.swing.JFrame {

      /** Creates new form Style1 */
      public Style2() {
      initComponents();
      }

      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      jPanel1 = new javax.swing.JPanel();
      cmd_SH = new javax.swing.JButton();
      pnl_2 = new javax.swing.JPanel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      cmd_SH.setText("Hide");
      cmd_SH.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      cmd_SHActionPerformed(evt);
      }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(558, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );
      jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(236, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );

      pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2);
      pnl_2.setLayout(pnl_2Layout);
      pnl_2Layout.setHorizontalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 621, Short.MAX_VALUE)
      );
      pnl_2Layout.setVerticalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 270, Short.MAX_VALUE)
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addContainerGap())
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      .addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(17, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {


      System.out.println(evt.getActionCommand());
      if (evt.getActionCommand().equals("Hide")) {
      pnl_2.setVisible(false);
      cmd_SH.setText("Show");
      this.setSize(643, 294);
      this.pack();


      }
      if (evt.getActionCommand().equals("Show")) {
      pnl_2.setVisible(true);
      cmd_SH.setText("Hide");
      this.setSize(643, 583);
      this.pack();
      }
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
      new Style1().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton cmd_SH;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel pnl_2;
      // End of variables declaration
      }





      share|improve this answer





















      • 5





        Please don't just provide code-only answers.

        – Lee Taylor
        Nov 22 '12 at 23:52
















      1














      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      /*
      * Style1.java
      *
      * Created on May 5, 2011, 6:31:16 AM
      */
      package Test;

      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JOptionPane;

      /**
      *
      * @author Sameera
      */
      public class Style2 extends javax.swing.JFrame {

      /** Creates new form Style1 */
      public Style2() {
      initComponents();
      }

      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      jPanel1 = new javax.swing.JPanel();
      cmd_SH = new javax.swing.JButton();
      pnl_2 = new javax.swing.JPanel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      cmd_SH.setText("Hide");
      cmd_SH.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      cmd_SHActionPerformed(evt);
      }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(558, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );
      jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(236, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );

      pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2);
      pnl_2.setLayout(pnl_2Layout);
      pnl_2Layout.setHorizontalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 621, Short.MAX_VALUE)
      );
      pnl_2Layout.setVerticalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 270, Short.MAX_VALUE)
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addContainerGap())
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      .addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(17, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {


      System.out.println(evt.getActionCommand());
      if (evt.getActionCommand().equals("Hide")) {
      pnl_2.setVisible(false);
      cmd_SH.setText("Show");
      this.setSize(643, 294);
      this.pack();


      }
      if (evt.getActionCommand().equals("Show")) {
      pnl_2.setVisible(true);
      cmd_SH.setText("Hide");
      this.setSize(643, 583);
      this.pack();
      }
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
      new Style1().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton cmd_SH;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel pnl_2;
      // End of variables declaration
      }





      share|improve this answer





















      • 5





        Please don't just provide code-only answers.

        – Lee Taylor
        Nov 22 '12 at 23:52














      1












      1








      1







      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      /*
      * Style1.java
      *
      * Created on May 5, 2011, 6:31:16 AM
      */
      package Test;

      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JOptionPane;

      /**
      *
      * @author Sameera
      */
      public class Style2 extends javax.swing.JFrame {

      /** Creates new form Style1 */
      public Style2() {
      initComponents();
      }

      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      jPanel1 = new javax.swing.JPanel();
      cmd_SH = new javax.swing.JButton();
      pnl_2 = new javax.swing.JPanel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      cmd_SH.setText("Hide");
      cmd_SH.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      cmd_SHActionPerformed(evt);
      }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(558, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );
      jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(236, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );

      pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2);
      pnl_2.setLayout(pnl_2Layout);
      pnl_2Layout.setHorizontalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 621, Short.MAX_VALUE)
      );
      pnl_2Layout.setVerticalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 270, Short.MAX_VALUE)
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addContainerGap())
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      .addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(17, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {


      System.out.println(evt.getActionCommand());
      if (evt.getActionCommand().equals("Hide")) {
      pnl_2.setVisible(false);
      cmd_SH.setText("Show");
      this.setSize(643, 294);
      this.pack();


      }
      if (evt.getActionCommand().equals("Show")) {
      pnl_2.setVisible(true);
      cmd_SH.setText("Hide");
      this.setSize(643, 583);
      this.pack();
      }
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
      new Style1().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton cmd_SH;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel pnl_2;
      // End of variables declaration
      }





      share|improve this answer















      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      /*
      * Style1.java
      *
      * Created on May 5, 2011, 6:31:16 AM
      */
      package Test;

      import javax.swing.JButton;
      import javax.swing.JFileChooser;
      import javax.swing.JOptionPane;

      /**
      *
      * @author Sameera
      */
      public class Style2 extends javax.swing.JFrame {

      /** Creates new form Style1 */
      public Style2() {
      initComponents();
      }

      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      jPanel1 = new javax.swing.JPanel();
      cmd_SH = new javax.swing.JButton();
      pnl_2 = new javax.swing.JPanel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      cmd_SH.setText("Hide");
      cmd_SH.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      cmd_SHActionPerformed(evt);
      }
      });

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(558, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );
      jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
      .addContainerGap(236, Short.MAX_VALUE)
      .addComponent(cmd_SH)
      .addContainerGap())
      );

      pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

      javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2);
      pnl_2.setLayout(pnl_2Layout);
      pnl_2Layout.setHorizontalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 621, Short.MAX_VALUE)
      );
      pnl_2Layout.setVerticalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 270, Short.MAX_VALUE)
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addContainerGap())
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      .addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addContainerGap(17, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {


      System.out.println(evt.getActionCommand());
      if (evt.getActionCommand().equals("Hide")) {
      pnl_2.setVisible(false);
      cmd_SH.setText("Show");
      this.setSize(643, 294);
      this.pack();


      }
      if (evt.getActionCommand().equals("Show")) {
      pnl_2.setVisible(true);
      cmd_SH.setText("Hide");
      this.setSize(643, 583);
      this.pack();
      }
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
      new Style1().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton cmd_SH;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel pnl_2;
      // End of variables declaration
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 8 '13 at 16:54









      Roddy of the Frozen Peas

      7,36782959




      7,36782959










      answered May 6 '11 at 2:52









      DDSameeraDDSameera

      111




      111








      • 5





        Please don't just provide code-only answers.

        – Lee Taylor
        Nov 22 '12 at 23:52














      • 5





        Please don't just provide code-only answers.

        – Lee Taylor
        Nov 22 '12 at 23:52








      5




      5





      Please don't just provide code-only answers.

      – Lee Taylor
      Nov 22 '12 at 23:52





      Please don't just provide code-only answers.

      – Lee Taylor
      Nov 22 '12 at 23:52











      0














      Call parent.remove(panel), where parent is the container that you want the frame in and panel is the panel you want to add.






      share|improve this answer




























        0














        Call parent.remove(panel), where parent is the container that you want the frame in and panel is the panel you want to add.






        share|improve this answer


























          0












          0








          0







          Call parent.remove(panel), where parent is the container that you want the frame in and panel is the panel you want to add.






          share|improve this answer













          Call parent.remove(panel), where parent is the container that you want the frame in and panel is the panel you want to add.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '13 at 21:24









          Mad PhysicistMad Physicist

          38.2k1678110




          38.2k1678110























              0














              If you want to hide panel on button click, write below code in JButton Action.
              I assume you want to hide jpanel1.



              jpanel1.setVisible(false);





              share|improve this answer




























                0














                If you want to hide panel on button click, write below code in JButton Action.
                I assume you want to hide jpanel1.



                jpanel1.setVisible(false);





                share|improve this answer


























                  0












                  0








                  0







                  If you want to hide panel on button click, write below code in JButton Action.
                  I assume you want to hide jpanel1.



                  jpanel1.setVisible(false);





                  share|improve this answer













                  If you want to hide panel on button click, write below code in JButton Action.
                  I assume you want to hide jpanel1.



                  jpanel1.setVisible(false);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 6:09









                  ShivBuyyaShivBuyya

                  9301213




                  9301213






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4286759%2fhow-to-show-hide-jpanels-in-a-jframe%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python