Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:57:18+00:00 2026-05-22T16:57:18+00:00

How do I make the subpanels within my main panel stay where they are

  • 0

How do I make the subpanels within my main panel stay where they are when I set one of the subpanels to be invisible?

What I have looks like:

[ (Panel1) (Panel2) (Panel3) (Panel4) ]

When I do panel3.setVisible(false) it then looks like:

[      (Panel1) (Panel2) (Panel4)     ]

I would like it to look like:

[ (Panel1) (Panel2)          (Panel4) ]

I am using the GridBagLayout and my mainPanel declaration looks like:

final JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

and I add an new panel like:

final JTextField valueTextField = new JTextField();
valueTextField.setPreferredSize(new Dimension(80, 25));
valueTextField.setName("Value");
c.gridx =0;
panel.add(valueTextField, c);

I’ll provide more code if needed and I don’t care which layout I use as long as it gets me what I want.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:57:19+00:00Added an answer on May 22, 2026 at 4:57 pm

    I suggest using a CardLayout within the individual cells, and instead of setting it to invisible, switch to an empty panel instead.

    The code below demonstrates this. Within hidePanel() there are two options to hide the cell with the CardLayout route currently enabled.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class InvisiblePanels {
        public static void main(String... args) throws Exception {
            JFrame frame = new JFrame();
            frame.setLayout(new GridBagLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            frame.add(new MyPanel(), c);
            c.gridx = 1;
            frame.add(new MyPanel(), c);
            c.gridx = 2;
            frame.add(new MyPanel(), c);
    
            frame.pack();
            frame.setVisible(true);
    
        }
    
        private static class MyPanel extends JPanel {
    
            CardLayout layout;
    
            public MyPanel() {
                layout = new CardLayout();
                setLayout(layout);
                JButton button = new JButton("Click me");
                button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        hidePanel();
                    }
                });
                add(button, "visible");
                add(new JPanel(), "invisible");
                layout.show(this, "visible");
            }
    
            public void hidePanel() {
    //            setVisible(false);
                layout.show(this, "invisible");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i make one class file for jar. and this class have use wurfl. and
make /bin/sh /Users/myusername/Downloads/php-5.3.5/ext/mssql/libtool --mode=compile cc -I. -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -DPHP_ATOM_INC -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/include -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/main -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
Make it on Linux. The reason to use more than one version of Vim,
I make an app that have to printing image. I need to print image
I have 2 click event functions that are practically identical... one works and the
To make my code flexible I would like to find a way to get
I make iphone application. I use for root controller tab bar controller. One of
I make an AJAX request like so using JQuery: $.ajax({ type: GET, url: getvideo.php,
Make something like in html/css
I make an arbitrary change to a file within my git working directory. git

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.