4

I have many jPanel (panelF) that are added in other jPanel(panelB). jPanelF contain jPanelC. enter image description here

I have set the layout to add them as follows:

    PanelB.setLayout(new BoxLayout(this.PanelB, BoxLayout.PAGE_AXIS));
    PanelF panelF1 = new PanelF(); 
    PanelB.add(panelF1);
    PanelF panelF2 = new PanelF(); 
    PanelB.add(panelF2);

I set visible false in jPanelC1. But JPanelF2 preserves distance and not want to make that blank space

enter image description here What I want is that when disappearing JPanelC1. Distance is maintained between the jPanelF and not be a white space. I tried validate() and change the layout but I fail. What would be the way? Thank you very much. Sorry for my English

know if there are something like $("#panelC1").fadeOut("slow")? would be ideal.

user60108
  • 3,270
  • 2
  • 27
  • 43
  • I think this is how `BoxLayout` works... – MadProgrammer Oct 03 '13 at 00:00
  • 1
    Personally, I would try using the `VerticalLayout` from SwingX – MadProgrammer Oct 03 '13 at 00:17
  • That layout should use then? I'm thinking of using a list, because not worked any layout – user60108 Oct 03 '13 at 00:21
  • 1
    Based on what you are describing, `VerticalLayout` can achieve what you want. It lays out components as a "list" (essentially), so they are anchored to the top the parent container. It relies on the preferred height of the components so when you hide a child component, the layout reflects these changes... – MadProgrammer Oct 03 '13 at 00:22
  • Thank you very much for your help, but I fail. I have imported import org.jdesktop.swingx.VerticalLayout, I added the layout PanelB.setLayout (new VerticalLayout ()). even I did validate () to disappear PanelC1 – user60108 Oct 03 '13 at 00:38

3 Answers3

3

Invoking panelC1.setVisible(false) makes panelC1 invisible, but it doesn't change the geometry defined by the get[Preferred|Maximum|Minimum]Size methods. You can

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

(This is a demonstration of vertical layout, not an answer per se).

Based on how I understand you problem, VerticalLayout may be a preferred solution...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import org.jdesktop.swingx.VerticalLayout;

public class VerticalLayoutTest {

    public static void main(String[] args) {
        new VerticalLayoutTest();
    }

    public VerticalLayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new VerticalLayout());

            JPanel outter = new JPanel(new BorderLayout());
            outter.setBorder(new LineBorder(Color.BLACK));
            outter.add(new JLabel("Outter 1"), BorderLayout.NORTH);
            JPanel inner = new JPanel(new GridBagLayout());
            inner.setBackground(Color.GREEN);
            inner.add(new JLabel("Inner 1"));
            outter.add(inner);
            add(outter);

            inner.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    Component component = e.getComponent();
                    component.setVisible(false);
                    component.invalidate();
                    component.validate();
                    revalidate();
                    repaint();
                }

            });

            add(Box.createVerticalGlue());

            outter = new JPanel(new BorderLayout());
            outter.setBorder(new LineBorder(Color.BLACK));
            outter.add(new JLabel("Outter 1"), BorderLayout.NORTH);
            inner = new JPanel(new GridBagLayout());
            inner.setBackground(Color.GREEN);
            inner.add(new JLabel("Inner 1"));
            outter.add(inner);
            add(outter);

        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

If I understood you correctly, you want panelF1 to maintain its size when the panel inside it is hidden. There are several ways to accomplish this. Depending on how your layout is set up, you could call setMinimumSize() on panelF1 and make sure its minimum size is slightly larger than panelC1's preferred size.

A more flexible (and likely more appropriate) way to do this, though, is via the layout manager itself. Read the tutorial on BoxLayout, and if it doesn't do what you want it to, try using a different layout manager. Both GroupLayout and GridBagLayout are very flexible, but they can also be difficult to manage. I've recently become somewhat of a fan of MigLayout.

EDIT

OK, guess I misunderstood your question due to how the layout of the pictures and text ended up. It's actually easier than I thought, then. Look at the BoxLayout tutorial I linked - what I think you're looking for is:

PanelB.add(panelF1);
PanelB.add(Box.createVerticalGlue()):
PanelB.add(panelF2);
Josh
  • 1,563
  • 11
  • 16