Resize layout when JPanel disappears

I have a lot of jPanel (panelF) that are added to another jPanel (panelB). jPanelF contains jPanelC. enter image description here

I 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 the visible value to false in jPanelC1. But JPanelF2 keeps the distance and does not want to make this empty space

enter image description here I want JPanelC1 to disappear. The distance is maintained between jPanelF and there will be no space. I tried validate () and changed the layout, but I am failing. What will it be? Many thanks. sorry for my English

you know if there is something like $ ("# panelC1"). fadeOut ("slow")? would be perfect.

+4
source share
3

,

, , VerticalLayout ...

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

        }
    }
}
+3

panelC1.setVisible(false) panelC1 , , get[Preferred|Maximum|Minimum]Size.

+3

, , panelF1 , . . , , setMinimumSize() F1 , , , PanelC1.

(, , ) , , . BoxLayout, , , . GroupLayout GridBagLayout , . MigLayout.

OK, I think I misunderstood your question because of how the design of images and text ended. Actually it was easier than I thought. Take a look at the BoxLayout tutorial I linked - I think you're looking for:

PanelB.add(panelF1);
PanelB.add(Box.createVerticalGlue()):
PanelB.add(panelF2);
0
source

All Articles