Java Tutorial/Swing Event/ChangeListener
Содержание
- 1 Get value of BoundedRangeModel in its change listener
- 2 Listening to JMenu Events with a ChangeListener: register a ChangeListener with a JMenu
- 3 Registers a change listener on a slider that controls animation speed. The change listener ignores the change events until the user releases the slider.
- 4 Tab change listener
- 5 Uses a change listener on a date-field spinner to change the color of the text as the spinner"s date changes.
- 6 Uses a change listener on the selection model of a color chooser to learn when the user changes the current color.
Get value of BoundedRangeModel in its change listener
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main extends JFrame {
JSlider slider = new JSlider();
BoundedRangeModel model = slider.getModel();
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(slider, BorderLayout.CENTER);
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Value: " + model.getValue());
}
});
pack();
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}
Listening to JMenu Events with a ChangeListener: register a ChangeListener with a JMenu
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ContructMenuWithAction {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
fileMenu.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("File Menu Changed");
}
});
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New");
fileMenu.add(newMenuItem);
newMenuItem.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("new menu item changed");
}
});
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Registers a change listener on a slider that controls animation speed. The change listener ignores the change events until the user releases the slider.
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RegisterChangeListenerToJSlider {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 10, 100, 20);
framesPerSecond.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
if (!source.getValueIsAdjusting()) {
int fps = (int) source.getValue();
System.out.println(fps);
}
}
});
frame.add(framesPerSecond,"North");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Tab change listener
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test extends JFrame {
JTabbedPane tabs = new JTabbedPane();
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(new JButton("Button on Tab 0"));
tabs.addTab("Tab 0", p);
p = new JPanel();
p.add(new JTextField("Text on Tab 1"));
tabs.addTab("Tab 1", p);
tabs.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Tab=" + tabs.getSelectedIndex());
}
});
getContentPane().add(tabs, "Center");
pack();
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
Uses a change listener on a date-field spinner to change the color of the text as the spinner"s date changes.
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AddingChangeListenerToJSpinner {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSpinner dateSpinner = new JSpinner();
dateSpinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
SpinnerModel dateModel = dateSpinner.getModel();
System.out.println(dateModel.getValue());
}
});
frame.add(dateSpinner,"North");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Uses a change listener on the selection model of a color chooser to learn when the user changes the current color.
import java.awt.Color;
import javax.swing.JColorChooser;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AddingChangeListenerToJColorChooser {
public static void main(String[] a) {
final JColorChooser tcc = new JColorChooser();
tcc.getSelectionModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println(tcc.getColor());
}
});
tcc.showDialog(null,"title",Color.RED);
}
}