Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not an appropriate use of <code>updateUI()</code>, which "Resets the UI property to a value from the current look and feel." Using <code>revalidate()</code>, as suggested in a comment, would be helpful only if components are added to, or removed from, the enclosing <code>Container</code>. Instead, invoke <code>repaint()</code> directly on the sub-panel instance. For greater flexibility, use the observer pettern suggested <a href="https://stackoverflow.com/a/12613725/230513">here</a>.</p> <p>Addendum: This example use <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html" rel="nofollow noreferrer"><code>Action</code></a> to encapsulate the button's behavior. Because the checkbox's selected state is a bound property, the component is repainted automatically, but you can invoke <code>repaint()</code> explicitly if needed.</p> <p>Addendum: Update to pass a reference as a parameter.</p> <p>Addendum: In this variation, the parameter is a reference to the exported <code>Action</code>.</p> <pre><code>import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; /** @see https://stackoverflow.com/a/14412516/230513 */ public class Example { private void display() { JFrame f = new JFrame("Example"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(0, 1)); JPanel panel = new JPanel(); final JCheckBox check = new JCheckBox("Check"); Action checkAction = new AbstractAction("Update") { @Override public void actionPerformed(ActionEvent e) { check.setSelected(!check.isSelected()); } }; panel.add(check); f.add(panel); f.add(new SubPanel(checkAction)); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static class SubPanel extends JPanel { public SubPanel(final Action action) { this.add(new JButton(action)); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Example().display(); } }); } } </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