SetVisible (false) changes the location of my components in my panel

How to make subpanels inside my main panel, where are they when I set one of the subpanels to invisible?

What do I see:

[ (Panel1) (Panel2) (Panel3) (Panel4) ] 

When I do panel3.setVisible(false) , it looks like this:

 [ (Panel1) (Panel2) (Panel4) ] 

I would like it to look like this:

 [ (Panel1) (Panel2) (Panel4) ] 

I am using GridBagLayout and my mainPanel application looks like this:

 final JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); 

and I add a new panel, for example:

 final JTextField valueTextField = new JTextField(); valueTextField.setPreferredSize(new Dimension(80, 25)); valueTextField.setName("Value"); c.gridx =0; panel.add(valueTextField, c); 

I will provide more code if necessary, and I don't care which layout I use until it gets me what I want.

+6
source share
3 answers

I suggest using CardLayout in separate cells, and instead of setting it to invisible, instead switch to an empty panel.

The code below demonstrates this. Inside hidePanel() there are two ways to hide a cell with the included CardLayout route.

 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"); } } } 
+6
source

I believe that all layout managers respect component visibility and do not include invisible components in their preferred size and layout calculations.

One solution could be to combine all of your panels into a panel using OverlayLayout:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OverlayLayoutInvisible { public static void main(String[] args) { JPanel panel = new JPanel(); panel.add( createPanel("Button 1") ); panel.add( createPanel("Button 2") ); panel.add( createPanel("Button 3") ); panel.add( createPanel("Button 4") ); panel.add( createPanel("Button 5") ); JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add( panel ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } public static JPanel createPanel(String text) { JButton button = new JButton( text ); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Component c = (Component)e.getSource(); c.setVisible(false); } }); InvisibleComponent ic = new InvisibleComponent( button ); JPanel panel = new JPanel(); panel.setLayout( new OverlayLayout(panel) ); panel.add( ic ); panel.add( button ); return panel; } public static class InvisibleComponent extends JComponent { private Component master; public InvisibleComponent(Component master) { this.master = master; setAlignmentX( master.getAlignmentX() ); setAlignmentY( master.getAlignmentY() ); } public Dimension getPreferredSize() { return master.getPreferredSize(); } } } 
+4
source

You might be able to configure GridLayout (do you have SSCCE?)

Otherwise:

Place Panel3 and Panel4 together in the same panel that you add to the GridBagLayout. Then configure a new panel in the layout, for example FlowLayout (left alignment with the preferred size), BorderLayout, GridLayout, etc.

+2
source

Source: https://habr.com/ru/post/889129/


All Articles