< Day Day Up > |
8.3 Working with SlidersAnother handy SWT control is the slider, which lets the user select a value from a continuous numeric range. Sliders are easy to use; this next example will recover a slider's new position when the user moves the slider's thumb (also called the slider's scrollbox). Here are the styles you can use when creating sliders:
We'll add a prompt to the user in a label ("Move the slider"), a horizontal slider using the style SWT.HORIZONTAL (use SWT.VERTICAL to create a vertical slider instead), and a text control to display the new position of the slider: final Label label = new Label(shell, SWT.NONE); label.setText("Move the slider"); label.setBounds(0, 20, 150, 15); final Slider slider = new Slider(shell, SWT.HORIZONTAL); slider.setBounds(0, 40, 200, 20); final Text text = new Text(shell, SWT.BORDER); text.setBounds(0, 100, 200, 25); That adds the slider; the next step is to handle user actions. Sliders support a number of events, each of which is given by an SWT constant:
You use the event object's detail member to determine which of these events occurred, as here in a switch statement, where we're displaying what event occurred in the application's text control: slider.addListener(SWT.Selection, new Listener( ) { public void handleEvent(Event event) { String outString = "Event: SWT.NONE"; switch(event.detail) { case SWT.ARROW_DOWN: outString = "Event: SWT.ARROW_DOWN"; break; case SWT.ARROW_UP: outString = "Event: SWT.ARROW_UP"; break; case SWT.DRAG: outString = "Event: SWT.DRAG"; break; case SWT.END: outString = "Event: SWT.END"; break; case SWT.HOME: outString = "Event: SWT.HOME"; break; case SWT.PAGE_DOWN: outString = "Event: SWT.PAGE_DOWN"; break; case SWT.PAGE_UP: outString = "Event: SWT.PAGE_UP"; break; } . . . } }); To determine the slider's current position, you use the getSelection method, as you see in the listing for Example 8-3. Example 8-3. Using SWT sliderspackage org.eclipsebook.ch08; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class Ch08_03 { public static void main(String [] args) { Display display = new Display( ); Shell shell = new Shell(display); shell.setText("Sliders"); shell.setSize(300, 200); final Label label = new Label(shell, SWT.NONE); label.setText("Move the slider"); label.setBounds(0, 20, 150, 15); final Slider slider = new Slider(shell, SWT.HORIZONTAL); slider.setBounds(0, 40, 200, 20); final Text text = new Text(shell, SWT.BORDER); text.setBounds(0, 100, 200, 25); slider.addListener(SWT.Selection, new Listener( ) { public void handleEvent(Event event) { String outString = "Event: SWT.NONE"; switch(event.detail) { case SWT.ARROW_DOWN: outString = "Event: SWT.ARROW_DOWN"; break; case SWT.ARROW_UP: outString = "Event: SWT.ARROW_UP"; break; case SWT.DRAG: outString = "Event: SWT.DRAG"; break; case SWT.END: outString = "Event: SWT.END"; break; case SWT.HOME: outString = "Event: SWT.HOME"; break; case SWT.PAGE_DOWN: outString = "Event: SWT.PAGE_DOWN"; break; case SWT.PAGE_UP: outString = "Event: SWT.PAGE_UP"; break; } outString += " Position: " + slider.getSelection( ); text.setText(outString); } }); shell.open( ); while(!shell.isDisposed( )) { if(!display.readAndDispatch( )) display.sleep( ); } display.dispose( ); } } You can see the results in Figure 8-5, where the user is dragging the thumb in the slider and the code is indicating that a drag event has occurred and the new location of the slider thumb. Figure 8-5. Dragging the thumb in a sliderWhen the user clicks the scrollbar, a page up or page down event occurs, as shown in Figure 8-6, and the thumb moves to a new position (the thumb's page increment is set to 10 by default). Figure 8-6. Creating a page down eventBesides using getSelection as we've done here, you can also use the setSelection method to set the current location of the slider. For example, if the user selects a page they want to jump to in a document, you can move the slider's position to match with the setSelection method.
|
< Day Day Up > |