Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Controlling Tick Marks

The JSlider class can be configured to display both major and minor tick marks. The setMajorTickSpacing and setMinorTickSpacing methods specify the tick mark spacing. The parameter is the number of units between tick marks. The number of tick marks drawn depends on the range of the slider and the spacing parameter. A major tick mark is drawn at the minimum value, and at every major tick mark spacing interval after that until the slider’s maximum is reached. The equation for where tick marks are drawn is as follows:

TickLocation = minimum + n * spacing
Where n = 0,1, range/spacing

The same logic is used for the minor tick marks, except that the minor tick spacing parameter is used. Also, if both a minor and major tick mark land on the same value, the major tick mark is drawn.

Finally, the setPaintTicks method must be called with a parameter of true to have the slider paint the tick marks. The following code can be used in the SliderTest application to create and configure the slider to contain tick marks:

JSlider slider = new JSlider();
slider.setMajorTickSpacing( 20 );
slider.setMinorTickSpacing( 5 );
slider.setPaintTicks( true );

The resulting slider is shown in Figure 10.12.


Figure 10.12  A JSlider with tick marks.

The default range for the slider, 0–100, is still being used. Thus, with a major tick mark spacing of 20, major ticks appear at 0, 20, 40, 60, 80, and 100. Similarly, with the minor tick spacing set to 5, minor ticks are expected at 5, 10, 15, 25, 30, and so on, up to 95. Notice that minor ticks are not expected at 0, 20, and so on, where the major ticks are located.

The slider shown in Figure 10.12 allows the value to fall on any integer value from 0 to 100. Sometimes you want to allow values to be selected only where the tick marks are displayed. This can be achieved by passing true to the setSnapToTicks method. Passing false to this method allows any value to be selected again.

Controlling Labels

In the previous section, tick marks were added to the slider to indicate where a selected value lies in the range of possible values. However, the range still is not shown on the slider. Labels can be added to the slider by passing true to the setPaintLabels method. If the following line is added to the previous version of the SliderTest application, the slider will look as shown in Figure 10.13.

slider.setPaintLabels( true );


Figure 10.13  Labels added to a slider.

Figure 10.13 shows that a label has been added for each major tick mark. For labels to be drawn, the major tick mark spacing must be specified. However, the tick marks need not be painted. The labels in Figure 10.13 could be painted without the tick marks with the following slider configuration:

JSlider slider = new JSlider();
slider.setMajorTickSpacing( 20 );
slider.setPaintLabels( true );

If the values in the slider are to represent percentages, currency, or some other type of formatted number, you want the labels on the slider to show the number’s type. This can be achieved in a couple of different ways. One approach is to subclass the JSlider class and create the properly formatted labels. Another approach is to set the labels using the setLabelTable method. The approach taken depends on the current requirements.

For a one-shot formatted slider where the major tick mark spacing and range are not going to change, setting the label table may be appropriate. However, for a general-purpose slider where the major tick spacing and range need to be flexible, extending the JSlider class may be more appropriate. In the next sections we’ll look at each alternative.

Setting the Label Table

The label table in the JSlider class is an AWT dictionary that contains a mapping from an integer to a component. The slider uses the components in the dictionary as the labels for the slider. There must be a label component for every Integer value associated with each major tick mark.

To configure a percent slider with labels that correspond to the major tick spacings, use the code in Listing 10.18. The resulting slider is shown in Figure 10.14.

Listing 10.18 The PercentSliderTest Application

package com.foley.test;

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.border.*;

import com.foley.utility.ApplicationFrame;
/**
 * An application that displays a JSlider instance
 * in its frame.
 *
 * @author Mike Foley
 **/
public class PercentSliderTest extends Object {

    /**
     * Application entry point.
     * Create a frame, the list and display it.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        JFrame frame = new ApplicationFrame( “Percent Slider Test” );
        NumberFormat percent = NumberFormat.getPercentInstance();

        Hashtable table = new Hashtable();
        for( int i = 0; i <= 100; i += 20 ) {
            JLabel l = new JLabel(
                       percent.format( ( double )i / 100.0 ) );
            table.put( new Integer( i ), l );
        }
        JSlider slider = new JSlider();
        slider.setMajorTickSpacing( 20 );
        slider.setMinorTickSpacing( 5 );
        slider.setPaintTicks( true );
        slider.setPaintLabels( true );
        slider.setLabelTable( table );
        slider.setBorder( BorderFactory.createLoweredBevelBorder() );
        frame.getContentPane().add( slider, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    } // main

} // PercentSliderTest


Figure 10.14  The PercentSlider Test application.

In the PercentSliderTest application, the label table is created with the percent number formatter. One JLabel instance is created for each major tick on the slider. This creates a nice slider, but it is not very versatile. If the major tick spacing parameter is changed, so must the label table. For formatted sliders whose range or major tick spacing is subject to change, extending the JSlider class provides a better solution.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.