Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example using the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer"><em>observer pattern</em></a>, also seen <a href="https://stackoverflow.com/a/3072979/230513">here</a>, <a href="https://stackoverflow.com/a/9346946/230513">here</a> and <a href="https://stackoverflow.com/a/11832979/230513">here</a>. Note that it would also be possible to listen to the combo's model.</p> <p><img src="https://i.stack.imgur.com/sgH2U.png" alt="enter image description here"></p> <pre><code>import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.*; /** * @see http://en.wikipedia.org/wiki/Observer_pattern * @see https://stackoverflow.com/a/10523401/230513 */ public class PropertyChangeDemo { public PropertyChangeDemo() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.add(new ObserverPanel()); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { PropertyChangeDemo example = new PropertyChangeDemo(); } }); } } class ObserverPanel extends JPanel { private JLabel title = new JLabel("Value received: "); private JLabel label = new JLabel("null", JLabel.CENTER); public ObserverPanel() { this.setBorder(BorderFactory.createTitledBorder("ObserverPanel")); JPanel panel = new JPanel(new GridLayout(0, 1)); panel.add(title); panel.add(label); this.add(panel); ObservedPanel observed = new ObservedPanel(); observed.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) { String value = e.getNewValue().toString(); label.setText(value); } } }); this.add(observed); } } class ObservedPanel extends JPanel { public static final String PHYSICIST = "Physicist"; private static final String[] items = new String[]{ "Alpher", "Bethe", "Gamow", "Dirac", "Einstein" }; private JComboBox combo = new JComboBox(items); private String oldValue; public ObservedPanel() { this.setBorder(BorderFactory.createTitledBorder("ObservedPanel")); combo.addActionListener(new ComboBoxListener()); this.add(combo); } private class ComboBoxListener implements ActionListener { @Override public void actionPerformed(ActionEvent ae) { String newValue = (String) combo.getSelectedItem(); firePropertyChange(PHYSICIST, oldValue, newValue); oldValue = newValue; } } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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