Results 1 to 11 of 11
  1. #1
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default How Can I Hide a Panel But Preserve the Space

    I have eight panels that are one above the other and are the full width of the frame.

    I would like to hide some of them some of the time and then "bring them back".

    If I use panel1.setVisible(false) panel1 will disappear but then all the panels below it move up to take panel1's place.

    I don't want that to happen. I want just a blank area where panel1 was.

    Is there a way to do that?

    Thanks,

    Pete

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How Can I Hide a Panel But Preserve the Space

    Quote Originally Posted by petec View Post
    I have eight panels that are one above the other and are the full width of the frame.

    I would like to hide some of them some of the time and then "bring them back".

    If I use panel1.setVisible(false) panel1 will disappear but then all the panels below it move up to take panel1's place.

    I don't want that to happen. I want just a blank area where panel1 was.

    Is there a way to do that?

    Thanks,

    Pete
    You could wrap it in another JPanel with a predefined size, or you could use a component like Box.createGlue() to take its place.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: How Can I Hide a Panel But Preserve the Space

    Quote Originally Posted by petec View Post
    If I use panel1.setVisible(false) panel1 will disappear but then all the panels below it move up to take panel1's place.
    Huh! That surprises me actually, making them invisible is not the same as physically removing them from the container. Which layout managers do you use? What steps do you take to tell the GUI that it should update itself (revalidate(), pack(), etc.)?

    BTW: this is Swing, right?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Hide a Panel But Preserve the Space

    I did try putting them in a JPanel already and it didn't help. I am using the NetBeans GUI builder so that may be part of the problem.
    I unclicked the "Vertical Resizable" box and set the vertical size to a value arrived at by experiment. When I leave that box where I typed the size it reverts to "Default" but the JPanel stays the size I want.

    Then when I run it and do panel1.setVisible(false) everything still moves up.

    I will have to look into Box.createGlue(). It isn't available in the NewBeans toolbox. I guess I have to learn how to do GUI components using text.

    Pete

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: How Can I Hide a Panel But Preserve the Space

    (learning to) Building it by hand is a good choice, it gives you the most control and eventually you'll do it even faster than clicking, dragging, dropping, cursing, scraping, redoing, etc.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How Can I Hide a Panel But Preserve the Space

    This actually doesn't surprise me, I would expect setVisible() to readjust the gui. Might be counter-intuitive, but I think it's at least reliably counter-intuitive.

    What layout are you using? I know this is at least true for BorderLayout, BoxLayout, and FlowLayout. Not sure about GridLayout.

    I would recommend ditching the gui builder, as it's going to be impossible to debug your code without an SSCCE or MCVE.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Hide a Panel But Preserve the Space

    Quote Originally Posted by gimbal2 View Post
    Huh! That surprises me actually, making them invisible is not the same as physically removing them from the container. Which layout managers do you use? What steps do you take to tell the GUI that it should update itself (revalidate(), pack(), etc.)?

    BTW: this is Swing, right?
    Netbeans toolbox (Or they call it Palette" says "Swing" in the title of all the catagories.

    I have no idea of the answers to your other questions.
    I have searched and found that the "repositioning" action happens to many people. Some responses say to use "show/hide" but they are no longer supported.

    When Netbeans creates the GUI it creates a whole section of "Generated Code" (in my case nearly 800 lines) which I can't change. But I will try to answer your "(revalidate(), pack(), etc.)" questions by plunging through those 800 lines.

    Update. The damn NetBeans "Find" command only searches My code, not the "Generated Code". This will take longer then I thought.

    Pete
    Last edited by petec; 03-27-2014 at 03:58 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: How Can I Hide a Panel But Preserve the Space

    Quote Originally Posted by KevinWorkman View Post
    This actually doesn't surprise me, I would expect setVisible() to readjust the gui. Might be counter-intuitive, but I think it's at least reliably counter-intuitive.
    If it's counter-intuitive then we're both being counter-intuited(?).
    It took me a while to get the hang of Android's three states for a component...visible, hidden, and gone. I was hiding things and wondering where all the gaps were coming from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How Can I Hide a Panel But Preserve the Space

    It turns out it depends entirely on your layout. BoxLayout, FlowLayout, and BorderLayout will resize and move components to fill space. Gridlayout doesn't. Here's my SSCCE:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class VisibleTest {
    
    	
    	public static void main(String... args){
    		
    		JFrame frame = new JFrame("Visibility Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JButton button1 = new JButton("Toggle visibility");
    		final JButton button2 = new JButton("I turn invisible");
    		JButton button3 = new JButton("I'm just here");
    		
    		button1.addActionListener(new ActionListener(){
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				button2.setVisible(!button2.isVisible());
    			}
    		});
    		
    		JPanel panel = new JPanel();
    		//panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    		panel.setLayout(new GridLayout(1, 3));
    		
    		panel.add(button1);
    		panel.add(button2);
    		panel.add(button3);
    		
    		frame.setContentPane(panel);
    		frame.setSize(500, 100);	
    		frame.setVisible(true);
    	}
    	
    }
    OP- The answer might be to just use a GridLayout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Hide a Panel But Preserve the Space

    That sure looks like it works. I just implemented your (SSCCE) code.

    But in the mean time I was dinking around with the NetBeans implementation and discovered that if I put a check mark the JPanel Property "Vertical Resizable" then, it doesn't resize. Talk about counterintutive

    I will just plung ahead implementing the code. I am pretty sure this problem will come back. If it does I'll implement Kevin's GridLayout.

    Thanks, I'm on my way.

    Pete

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: How Can I Hide a Panel But Preserve the Space

    Amazing. I've been doing Swing for yonks and I -never- noticed this.

    ... I guess I never had a good reason to make stuff invisible, I'd just swap stuff out like replacing a filled panel with an empty panel.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JSP Not preserve content formats
    By pari in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-30-2012, 10:52 AM
  2. JList does not occupy all the space in the panel.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 10-03-2011, 09:07 PM
  3. Hide Panel when clicking outside of it
    By steeveesas in forum AWT / Swing
    Replies: 20
    Last Post: 08-14-2011, 12:10 AM
  4. Replies: 5
    Last Post: 07-12-2011, 06:43 PM
  5. Javascript I want to hide the space occupied by the row in tabls based on value selec
    By swapnalimore in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-12-2011, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •