To monitor slider input, you must have a class that implements the ChangeListener
interface in the javax.swing.event
package. This interface includes only one method:
public void stateChanged(ChangeEvent event); {
// statements to handle the event
}
To register an object as a change listener, call the addChangeListener(Object)
method of the container that holds the slider. When the slider is moved, the listening object’s stateChanged()
method is called.
This method is called with a ChangeEvent
object that can identify the slider component that changed in value. Call the object’s getSource()
method and cast the object to a JSlider
, as in the following statement:
JSlider changedSlider = (JSlider) event.getSource();
In this example, event
is the ChangeEvent
object that is an argument to the stateChanged()
method.
Change events occur throughout a slider’s movement. They begin when the slider is first moved, and they don’t stop occurring until the slider has been released. For this reason, you might not want to do anything in the stateChanged()
method until the slider has stopped moving.
To see if a slider is currently being moved around, call its getValueIsAdjusting()
method. This method returns true
while movement is taking place and false
otherwise.
This technique is demonstrated in your next project, a Java application that uses three sliders to choose a color. Colors are created in Java by using the Color
class in the java.awt
package.
One way to create a Color
object is to specify the amount of red, green, and blue in the color. Each of these can be an integer from 0 to 255 with 255 representing the maximum amount of that color.
The following statement creates a Color
object that represents the color butterscotch:
Color butterscotch = new Color(255, 204, 128);
The red value used to create this Color
object is 255, so it contains the maximum amount of red. It also contains a large amount of green and some blue.
Listing 16.2 contains the ColorSliders
application, which has three sliders, three labels for the sliders, and a panel where the color is displayed. Create a new empty Java file called ColorSliders
, enter the text of the listing in the source editor, and save the file.
1: import javax.swing.*;
2: import javax.swing.event.*;
3: import java.awt.*;
4:
5: public class ColorSliders extends JFrame implements ChangeListener {
6: ColorPanel canvas;
7: JSlider red;
8: JSlider green;
9: JSlider blue;
10:
11: public ColorSliders() {
12: super("Color Slide");
13: setLookAndFeel();
14: setSize(270, 300);
15: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16: setVisible(true);
17:
18: canvas = new ColorPanel();
19: red = new JSlider(0, 255, 255);
20: green = new JSlider(0, 255, 0);
21: blue = new JSlider(0, 255, 0);
22:
23: red.setMajorTickSpacing(50);
24: red.setMinorTickSpacing(10);
25: red.setPaintTicks(true);
26: red.setPaintLabels(true);
27: red.addChangeListener(this);
28:
29: green.setMajorTickSpacing(50);
30: green.setMinorTickSpacing(10);
31: green.setPaintTicks(true);
32: green.setPaintLabels(true);
33: green.addChangeListener(this);
34:
35: blue.setMajorTickSpacing(50);
36: blue.setMinorTickSpacing(10);
37: blue.setPaintTicks(true);
38: blue.setPaintLabels(true);
39: blue.addChangeListener(this);
40:
41: JLabel redLabel = new JLabel("Red: ");
42: JLabel greenLabel = new JLabel("Green: ");
43: JLabel blueLabel = new JLabel("Blue: ");
44: GridLayout grid = new GridLayout(4, 1);
45: FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
46: setLayout(grid);
47:
48: JPanel redPanel = new JPanel();
49: redPanel.setLayout(right);
50: redPanel.add(redLabel);
51: redPanel.add(red);
52: add(redPanel);
53:
54: JPanel greenPanel = new JPanel();
55: greenPanel.setLayout(right);
56: greenPanel.add(greenLabel);
57: greenPanel.add(green);
58: add(greenPanel);
59:
60: JPanel bluePanel = new JPanel();
61: bluePanel.setLayout(right);
62: bluePanel.add(blueLabel);
63: bluePanel.add(blue);
64: add(bluePanel);
65: add(canvas);
66:
67: setVisible(true);
68: }
69:
70: public void stateChanged(ChangeEvent event) {
71: JSlider source = (JSlider) event.getSource();
72: if (source.getValueIsAdjusting() != true) {
73: Color current = new Color(red.getValue(), green.getValue(),
74: blue.getValue());
75: canvas.changeColor(current);
76: canvas.repaint();
77: }
78: }
79:
80: public Insets getInsets() {
81: Insets border = new Insets(45, 10, 10, 10);
82: return border;
83: }
84:
85: private void setLookAndFeel() {
86: try {
87: UIManager.setLookAndFeel(
88: "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
89: );
90: } catch (Exception exc) {
91: // ignore error
92: }
93: }
94:
95: public static void main(String[] arguments) {
96: ColorSliders cs = new ColorSliders();
97: }
98: }
99:
100: class ColorPanel extends JPanel {
101: Color background;
102:
103: ColorPanel() {
104: background = Color.red;
105: }
106:
107: public void paintComponent(Graphics comp) {
108: Graphics2D comp2D = (Graphics2D) comp;
109: comp2D.setColor(background);
110: comp2D.fillRect(0, 0, getSize().width, getSize().height);
111: }
112:
113: void changeColor(Color newBackground) {
114: background = newBackground;
115: }
116: }
When you run the application, as shown earlier in Figure 16.2, a frame contains three sliders that represent the amount of red, green, and blue in a panel along the bottom edge of the frame.
Adjust the values of each slider to change the color that is displayed. In Figure 16.2, the application is used to create North Texas Mean Green (red 50, green 150, and blue 50). This shade inspires alumni of the University of North Texas to leap to our feet at sporting events and make ferocious eagle-claw hand gestures that turn visiting teams yellow (red 255, green 255, orange 0).
3.139.87.61