Java Reference
In-Depth Information
Let us go back to our fundamental questions. In Java 6, there are six constructors for
the JSlider class. You want to learn about one or two of them. You must strike a
balance somewhere between the trivial and the bizarre. Consider
public JSlider ()
Creates a horizontal slider with the range 0 to
100 and an initial value of 50.
Maybe that is good enough for now, but what if you want another range or initial
value? It seems too limited.
On the other side of the spectrum, there is
public JSlider (BoundedRangeModel brm)
Creates a horizontal slider using the specified
BoundedRangeModel.
Whoa! What is that? You can click on the BoundedRangeModel link to get a long
explanation of this class. This appears to be some internal mechanism for the Swing
implementors. Let's try to avoid this constructor if we can. Looking further, we find
public JSlider (int min, int max, int value)
Creates a horizontal slider using the specified
min, max, and value.
This sounds general enough to be useful and simple enough to be usable. You might
want to stash away the fact that you can have vertical sliders as well.
810
811
Next, you want to know what events a slider generates. There is no
addActionListener method. That makes sense. Adjusting a slider seems
different from clicking a button, and Swing uses a different event type for these
events. There is a method
public void addChangeListener(ChangeListener l)
Click on the ChangeListener link to find out more about this interface. It has a
single method
void stateChanged(ChangeEvent e)
Apparently, that method is called whenever the user moves the slider. What is a
ChangeEvent ? Once again, click on the link, to find out that this event class has no
Search WWH ::




Custom Search