Username:
Password:
Java 6 Tutorials

Swing Components / Sliders & Spinners

Subtitles of the Movie

Sliders and spinners both do the same things. They allow the user to choose one value from within a preset range. But they do it in very different ways. First let me show you a date spinner. It's always a problem to set things up so the user can enter a date. Because of the problems of validating the day of the month with the month name and then there's Leap Year and so on. The spinner date model component has partially solved this problem. To use it, you'll need to import both the JSpinner and the spinner date model classes and you'll need to implement the change listener interface. You begin by creating a new spinner date model object. Now, you can specify the default date or you can create it this way with no arguments and it will default to the current date. It is to this model object that you add the change listener so you will be notified when the user changes the date. But you create a different object for the display. You create a JSpinner object using the date model as the argument on the constructor. And it is this new JSpinner object that you put on display. This is the method that's called when the user changes the displayed date. Now, this example doesn't do anything but output the date. Here's how it works. It starts out by displaying the current date. You can make an adjustment to individual parts of the date. Whenever you change something in the display date, the other parts of the date change as necessary to keep the date valid. Now, you can see that this uses a two-digit number to display the date so it can only display the years in a 100-year range. It displays the years 20 years into the future and 80 years into the past. So it won't work for everything. If you need a wider range of dates, you'll have to come up with some other way. Probably the most common form of a spinner is one that lets the mouse choose from a range of numbers. To do that, you will need to import a different model for the spinner, the spinner number model. You will still need to implement the change listener. You construct the spinner number model by specifying first a starting value, in this case 180 and a range zero to 360 and a step. The step is the amount the value will move with each click of the mouse. You create a JSpinner object and add it to the display the same as before. In this example, a second component, a rotor is added to the display. All it does is draw a line slanted in the direction specified by the angle of value of the spinner. In the state change method, the value is retrieved from the spinner. It is a number and it comes to you in the form a an integer object. The value of the integer is passed to the rotor object, which is repainted in the position specified. Now, here's what that looks like. You can see the spinner here at the top of the window with its initial value of 180 and with the two buttons that are used to adjust the value. Selecting and holding one of the buttons will move the value through its range. Or you can simply enter a number directly. Another component that does exactly the same thing but in a very different way is the slider. The slider is easier to program. You will need to import the JSlider component and you will need to implement the change listener interface. The construction of a slider is straightforward. This example uses a horizontal slider that has a range of zero to 360 and has an initial setting of 180. This slider is to display tick marks along its length. It will display a major tick mark at 45 and a minor tick mark at every ten. This object is added to the list of change listeners and the slider is added to the display. A rotor is added to the bottom of the display. When the value is changed by the user, the new value is used to set the position of the rotor. Here's how it works. You can see the slider sitting at its midpoint, 180. Unlike the spinner, you are not shown the numeric value of the current setting, but it is much easier to set the value of a slider. Sliders and spinner do basically the same thing. Which one you use depends on what your application needs. With one you get speed, with the other you get accuracy. The next lesson is about decorating your application. There are several ways you can use icons to decorate your display.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available