Reputation: 3452
I have many jPanel (panelF) that are added in other jPanel(panelB).
jPanelF contain jPanelC.
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
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.
Upvotes: 4
Views: 499
Reputation: 347204
(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...
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);
}
}
}
Upvotes: 3
Reputation: 205785
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
Override the defining methods, as shown in How to Use BoxLayout: Specifying Component Sizes.
Remove panelC1
from the enclosing panelF1
, as shown here, and restore it when it is again visible.
Use a layout other than Box
.
Upvotes: 3
Reputation: 1553
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);
Upvotes: 0