Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a code example showing how to show/hide a grouped component, which is implemented as a <code>JPanel</code> as others have suggested. The <code>GroupedComponent</code> retains it's size even when hidden.</p> <p><img src="https://i.stack.imgur.com/Lanx5.png" alt="enter image description here"> &nbsp; <img src="https://i.stack.imgur.com/0sbo4.png" alt="enter image description here"></p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; public class SimpleTest extends JFrame { GroupedComponent test = new GroupedComponent("one", "two", "three"); public SimpleTest() { super("GroupedComponent Example"); JPanel content = (JPanel)getContentPane(); content.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); final JButton hideButton = new JButton(test.getButtonText()); hideButton.setPreferredSize(new Dimension(100,hideButton.getPreferredSize().height)); hideButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { test.toggle(); hideButton.setText(test.getButtonText()); } }); content.add(hideButton); content.add(test); } public static void main(String[] argv) { SwingUtilities.invokeLater(new Runnable() { public void run() { SimpleTest c = new SimpleTest(); c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.pack(); c.setVisible(true); } }); } class GroupedComponent extends JPanel { boolean visible = true; JTextField field; JButton button; JLabel label; GroupedComponent(String fieldText, String buttonText, String labelText) { super(new GridLayout(1, 3, 4, 4)); field = new JTextField(fieldText); button = new JButton(buttonText); label = new JLabel(labelText); add(field); add(button); add(label); setBorder(new CompoundBorder(new LineBorder(Color.lightGray), new EmptyBorder(4,4,4,4))); } void toggle() { if(visible) { visible = false; field.setVisible(false); button.setVisible(false); label.setVisible(false); } else { visible = true; field.setVisible(true); button.setVisible(true); label.setVisible(true); } } String getButtonText() { return visible ? "Hide" : "Show"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload