Chapter 16. SWT Slider and ProgressBar

Two useful interface elements are sliders and progress bars. Sliders permit the user to choose a value from within a range of values, simply by dragging the control with the mouse. The volume control found in many sound-enabled applications is an example of a slider.

Progress bars are useful when the user has initiated a long-running process such as a file transfer. The progress meter gives the user the ability to monitor the progress of the operation, to see how much of the operation is complete as well as how much remains.

The SWT provides the classes needed to easily include both sliders and progress bars in your applications.

The SWT Slider Class

The ability to create sliders in the SWT is encapsulated in a single class—Slider, part of the org.eclipse.swt.widgets package. Slider provides the ability to use a single widget to enable the user to specify a range of values, much like was done using the SWT.ARROW style of Button in Chapter 6. In fact, building a similar interface using Slider is a great way to examine the uses of the Slider class.

How do I do that?

To duplicate the SWT.ARROW Button example from Chapter 6, you must create a Shell and add two widgets—a Text to display the data and a Slider to enable the user to alter the data. Example 16-1 demonstrates how to do this.

Example 16-1. Using a Slider to obtain input

import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.