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

  • Home
  • SEARCH
  • 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

To make a JavaScript class with a public method I'd do something like: function
I have 2 click event functions that are practically identical... one works and the
I make some script, that have modal window on fancybox and on load it's
Routing make me crazy thees days :( I have two areas: CityPage and Job
I make code to display multiple pages (max. 5 lines/page) with persons from one
Make something like in html/css
Want make a mind map like the following: Image link is not available anymore.
make ARCH=arm msm_defconfig make ARCH=arm CROSS_COMPILE=$SOURCE_FOLDER/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi- zImage -j4 I would like to build android
? MAKE num RANDOM 1 ? IF [:num = 1] [print Number is one.]
To make my code flexible I would like to find a way to get

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.