JButton button = new JButton("say something");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("something");
}
});
SpinnerNumberModel model = new SpinnerNumberModel(10,0,100,1);
JSpinner spinner = new JSpinner(model);
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = ((Integer)((JSpinner)e.getSource()).getValue()).intValue();
tellSomeone(value);
}
});
// Although not an anonymous listener in the strict
// sense, this approach can make sense sometimes:
JButton someButton = new JButton("compute");
final JTextField textField = new JTextField(12);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = Integer.parseInt(textField.getText());
doSomething(n);
}
};
someButton.addActionListener(al);
textField.addActionListener(al);