JSliders allow the user to select from a range of integer values. It is usually operated with a mouse, however, once gained the focus, the left and right arrow keys cause moving the thumb one unit to the left or right, respectively.
The Slider interface is demonstrated below. Load this demo to run it.
The interface is supported by the following methods of class JSlider:
void setPaintTicks(boolean) | Shows or hides the ticks. |
void setPaintLabels(boolean) | Shows or hides the labels. |
void setPaintTrack(boolean) | Show or hides the track. |
void setMinorTickSpacing(int) | Sets the minor tick spacing. The number that is passed-in represents the distance, measured in values, between each minor tick mark. If you have a slider with a range from 0 to 50 and the minor tick spacing is set to 10, you will get minor ticks next to the following values: 0, 10, 20, 30, 40, 50. |
void setMajorTickSpacing(int) | Sets the major tick spacing. The number that is passed-in represents the distance, measured in values, between each major tick mark. If you have a slider with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50. |
int getValue() | Returns the slider value |
The method constructor takes several arguments. The first one specifies the slider orientation (vertical or horizontal), the next two specify the minimum and maximum values of the slider scale, and the last one specifies the default position of the thumb:
To handle the slider events you should implement the ChangeListener interface which is in the package javax.swing.event. A class implementing this interface must provide the following method:
This method is called if the slider thumb is moved.
Your task is to use the Slider to implement the following application. The slider controls the diameter of the drawn circle. Load this demo to see it in action.
To implement the ChangeListener interface you need to indicate this in the class header
class SwingApp extends JFrame implements ChangeListener |
... and provide the following method in this class:
public void stateChanged(ChangeEvent e) { } |
This class has only three members:
To construct the GUI is straightforward and needs no further comments. Make sure, however, that you understand the purpose of every line there:
pane.setLayout(new BorderLayout()); panel = new DrawPanel(10); // add GUI components panel.setBackground(Color.yellow); // add here more code for GUI components slider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 10); slider.setFont(new Font("TimesRoman", Font.PLAIN, 8)); slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setPaintTrack(true); slider.addChangeListener(this); pane.add(panel, BorderLayout.CENTER); pane.add(slider, BorderLayout.SOUTH); |
This method consists of a single statement, where we retrieve the value provided by the slider and use it to set up the circle diameter.
public void stateChanged(ChangeEvent e) { panel.diameter = slider.getValue(); panel.repaint(); } |
This class is intended to draw the circle. It has the only public member
... and two methods
Use the above ideas to implement the following applet that is used to set up the color components (RED, GREEN, and BLUE) and draw a string in this color. Check here to see it in action.