6.3 The JSlider Class


The JSlider class represents a graphical slider. Like scrollbars, sliders can have either a horizontal or vertical orientation. With sliders, however, you can enhance their appearance with tick marks and labels. The class hierarchy is illustrated in Figure 6-8. In most instances, a slider is preferable to a standalone scrollbar. Sliders represent a selection of one value from a bounded range. Scrollbars represent a range of values within a bounded range and are best used in things like the JScrollPane.

Figure 6-8. JSlider class diagram
figs/swng2.0608.gif

The JSlider class allows you to set the spacing of two types of tick marks: major and minor. Major tick marks are longer than minor tick marks and are generally used at wider intervals. Figure 6-9 shows various sliders that can be composed in Swing.

Figure 6-9. Various sliders in Swing
figs/swng2.0609.gif

The setPaintTicks( ) method sets a boolean, which is used to activate or deactivate the slider's tick marks. In some L&Fs, the slider changes from a rectangular shape to a pointer when tick marks are activated. This is often done to give the user a more accurate representation of where the slider falls.

You can create a Dictionary of Component objects to annotate the slider. Each entry in the Dictionary consists of two fields: an Integer key, which supplies the index to draw the various components, followed by the component itself. If you do not wish to create your own label components, you can use the createStandardLabels( ) method to create a series of JLabel objects for you. In addition, if you set the paintLabels property to true and give a positive value to the majorTickSpacing property, a set of labels that matches the major tick marks is automatically created. Figure 6-10 shows what a JSlider looks like in four L&Fs.

Figure 6-10. Sliders in several L&Fs
figs/swng2.0610.gif

6.3.1 Properties

Table 6-4 shows the properties of the JSlider component. The slider object has several properties in addition to those of its data model. The orientation property determines which way the slider moves. It can be one of two values: JSlider.HORIZONTAL or JSlider.VERTICAL.

Table 6-4. JSlider properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

   

JSlider.AccessibleJSlider( )

changeListeners1.4

ChangeListener[]

·

   

Empty array

extent

int

·

 

·

0

invertedb

boolean

·

 

·

false

labelTableb

Dictionary

·

 

·

null

majorTickSpacingb

int

·

 

·

10

maximumb

int

·

 

·

100

minimumb

int

·

 

·

0

minorTickSpacingb

int

·

 

·

2

modelb

BoundedRangeModel

·

 

·

DefaultBoundedRangeModel

orientationb

int

·

 

·

JSlider.HORIZONTAL

paintLabelsb

boolean

·

 

·

false

paintTicksb

boolean

·

 

·

false

paintTrackb

boolean

·

 

·

true

snapToTicksb

boolean

·

 

·

true

UIb

SliderUI

·

 

·

From L&F

UIClassIDo

String

·

   

"SliderUI"

value

int

·

 

·

50

valueIsAdjusting

boolean

·

 

·

false

1.4since 1.4, bbound, ooverridden

See also properties from the JComponent class (Table 3-6).

The labelTable is a Dictionary of slider values and JLabel objects. The labels in this dictionary are used to label the slider; they can be explicitly set by the user or generated automatically by calling createStandardLabels( ), which we'll discuss later. The paintLabels property is a boolean that determines whether to paint the textual labels associated with the slider. If paintLabels is set to true, the JLabel objects in the labelTable are painted at the appropriate locations in the slider.

The paintTicks property is a boolean; it decides if the major and minor tick marks are drawn. If it is true, both types of tick marks are drawn (unless their spacing is set to 0 see the last paragraph in this section). The snapToTicks property indicates whether the slider adjusts its value to the nearest tick. The paintTrack property controls whether the "track" on the slider is painted. If the inverted property is false, then the table increments from left to right or from bottom to top; if the property is true, the table increments from right to left or from top to bottom. All tick marks and labels are shifted accordingly.

The minimum , maximum, value, and valueIsAdjusting properties match the equivalent properties in the BoundedRangeModel of the slider. The extent property is slightly different from the model; it tells how much the slider increments up or down when L&F-specific keys are pressed (generally, PageUp and PageDown).

The majorTickSpacing and minorTickSpacing properties decide the repetition rate of the tick marks. In the event that both a major and minor tick mark occupy the same position, the major wins out. Neither property should ever be less than zero. If you want to prevent either type of tick mark from being drawn, give it a spacing value of 0.

6.3.1.1 Client properties

The JSlider object contains one client property that works only with the Metal L&F: JSlider.isFilled . When this client property is set to true, as shown in Figure 6-11, the result is a slider component that fills itself only on its descending half:

JSlider slider = new JSlider( ); slider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
Figure 6-11. JSlider with the isFilled client property set (Metal L&F)
figs/swng2.0611.gif

6.3.2 Events

JSlider triggers a ChangeEvent whenever the user modifies any of its properties. It also generates a PropertyChangeEvent whenever any of its properties change.

public void addChangeListener(ChangeListener l)
public void removeChangeListener(ChangeListener l)

Add or remove a specific listener from receiving property change events generated by the JSlider object.

6.3.3 Constructors

public JSlider( )
public JSlider(int orientation)
public JSlider(int min, int max)
public JSlider(int min, int max, int value)
public JSlider(int orientation, int minimum, int maximum, int value)
public JSlider(BoundedRangeModel brm)

Set the initial values of the slider. The orientation must be either JSlider.HORIZONTAL or JSlider.VERTICAL. If anything else is passed in, the JSlider object throws a runtime IllegalArgumentException. The remaining parameters are used to initialize the slider's bounded-range model. If the parameters are not given, they are initialized to the default values in Table 6-4. The final constructor accepts a bounded-range model object to initialize the slider.

6.3.4 Labels

public Hashtable createStandardLabels(int increment)
public Hashtable createStandardLabels(int increment, int start)

Utility functions that create a hashtable of numeric labels, starting at the value specified by start (or the minimum if omitted), and incrementing by the value specified by increment. The resulting Hashtable can be placed in the labelTable property, and its labels are drawn on the slider if the drawLabels property is set to true.

6.3.5 Miscellaneous

public void updateUI( )

Signal that a new L&F has been set using the setUI( ) accessor. Invoking this method forces the slider component to reset its view using the new UI delegate.

6.3.6 Creating a Slider

The following program shows how to create a full-featured slider:

// SliderExample.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class SliderExample extends JPanel {     public SliderExample( ) {         super(true);         this.setLayout(new BorderLayout( ));         JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);         slider.setMinorTickSpacing(2);         slider.setMajorTickSpacing(10);         slider.setPaintTicks(true);         slider.setPaintLabels(true);         // We'll use just the standard numeric labels for now.         slider.setLabelTable(slider.createStandardLabels(10));         add(slider, BorderLayout.CENTER);     }     public static void main(String s[]) {          JFrame frame = new JFrame("Slider Example");          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          frame.setContentPane(new SliderExample( ));          frame.pack( );          frame.setVisible(true);     } }

This code yields the slider shown in Figure 6-12.

Figure 6-12. Swing slider
figs/swng2.0612.gif


Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net