Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><p>great lesson, how the <code>LayoutManager</code> works, only <code>GridLayout</code> can do that without any issue, but this is its property</p></li> <li><p>last <code>JComponent</code> in the row or column (part of then is about) <code>can't be invisible</code>, then container is shrinked </p></li> <li><p>easiest work_around is to display container, then to call <code>setVisible(false)</code> wrapped into <code>invokeLater</code></p></li> </ul> <p><img src="https://i.stack.imgur.com/O1RJp.jpg" alt="enter image description here"> ... .... <img src="https://i.stack.imgur.com/C7V25.jpg" alt="enter image description here"></p> <pre><code>import java.awt.event.ItemEvent; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Evaluator { private JFrame frame = new JFrame(); private JPanel panel = new JPanel(); private JCheckBox checkBox = new JCheckBox(); private JTextField textField = new JTextField(10); public Evaluator() { checkBox.setText("Properties"); checkBox.addItemListener(new java.awt.event.ItemListener() { @Override public void itemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { textField.setVisible(true); } else { textField.setVisible(false); } } }); //panel.setLayout(new GridLayout()); //panel.setLayout(new SpringLayout()); //panel.setLayout(new BorderLayout()); //panel.setLayout(new GridBagLayout()); //panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); panel.add(checkBox/*, BorderLayout.NORTH*/); panel.add(textField/*, BorderLayout.SOUTH*/); //panel.doLayout(); //textField.setVisible(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { textField.setVisible(false); } }); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { Evaluator evaluator = new Evaluator(); } }); } } </code></pre>
 

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