June 19, 2018
Hot Topics:

SWT Programming with Eclipse

  • March 25, 2004
  • By Koray Guclu
  • Send Email »
  • More Articles »
8.2.2. Slider, Scale, and ProgressBar widgets

Scale represents a range of selectable continues values. The range can be specified by the setMinimum() and setMaximum() methods of the Scale class. You can get the selection value by using the getSelection() method. A scale can only have one selected value at a given time. That is to say, it is not possible to have multiple selections.

Figure 6. Slider and Scale widgets inside a Group.

Depending on the parameters passed to the constractor, it is possible to create different scale and slider widgets. The slider and scale constants are shown in Table 5.

Table 5. SWT slider & scale style bar constants
Shows horizontal or vertical widget

Optionally, you can use the SWT.BORDER constant to create a border around a scale widget. This constant has no effect on the slider widget.

 Source 4. Slider widget example
 final Slider slider =   new Slider(shell,SWT.VERTICAL); slider.setMinimum(0); slider.setMaximum(100); slider.setIncrement(5); slider.setPageIncrement(10); slider.setSelection(25); slider.addSelectionListener(  new SelectionAdapter()  {    public void widgetSelected(SelectionEvent e)    {      System.out.println("Selection:"+                          slider.getSelection());    }  } );

The ProgressBar widget is similar to the Slider and Scale widgets, but it is not selectable. It shows the progress of a task. You can use the SWT.SMOOTH and SWT.INTERMINATE constants with the ProgressBar widget.

8.2.3. Text widget

A Text widget can be used to show or to edit text. Alternatively, you can use a StyledText widget to display the text by using a different font and color. The StyledText widget allows you to set the foreground, background color, and font for a given range within a text block.

Figure 7. Text widget.

It is possible to create a Text widget with the constants shown in Table 6. A Text widget is a scrollable widget. Therefore, the SWT.H_SCROLL and SWT.V_SCROLL constants can be used to add scroll bars to a Text widget.

Table 6. SWT Text style bit constants
Shows a single line or multi line widget
SWT.READ_ONLY Creates a read-only widget
SWT.WRAP Wraps the text

Source 5 is a simple Text widget usage example.

Source 5. Text widget example
 Text text =   new Text(shell, SWT.MULTI|SWT.WRAP);
8.2.4. List widget

A List widget can be used to display a list of selectable string values. In the case of a selection, the List object sends a notification event to its listeners. The type of a selection can be single or multiple selections. The type of the selection is determined by the SWT.SINGLE or SWT.MULTI constants. The List widget is a scrollable widget. Therefore, the SWT.H_SCROLL and SWT.V_SCROLL constants can be used to add scroll bars to a Text widget.

Figure 8. List widget.

The following code snippet shows a simple List widget example.

Source 6. List example
 final List list = new List(shell,SWT.MULTI); for (int i = 1; i < 11; i++)  {   list.add(i+".)www.korayguclu.de"); } list.addSelectionListener(   new SelectionAdapter()   {     public void widgetSelected(SelectionEvent e)     {       List list = (List) e.getSource();       String[] str = list.getSelection();       for (int i = 0; i < str.length; i++)       {         System.out.println("Selection: "+str[i]);       }     }  } );
8.2.5. Sash widget

A Sash widget can be used to display composite widgets in resizable areas. The following figure shows a Sash widget example.

Figure 9. Sash widget.

A basic sash example can be seen below.

Source 7. Sash example
 Button button = new Button(shell,SWT.PUSH); Sash sash = new Sash(shell, SWT.VERTICAL); Button button1 = new Button(shell,SWT.PUSH);

Page 4 of 6

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date