October 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

SWT Programming with Eclipse

  • March 25, 2004
  • By Koray Guclu
  • Send Email »
  • More Articles »

8.3. Composite Widgets

Composite widgets can contain other composite widgets. The Composite class is the parent class of the composite widgets.

Figure 10. Composite can contain other composite classes.

Composite classes can contain other composite classes. This containment is built up by using the constructor of a composite widget class. In contrast to Swing, there is no add() method; instead, you must use constructors to build up a containment structure.

As can be seen from Figure 10, the Shell class is also a composite class. That is to say, the Shell object can contain other composite classes.

Composite widgets are Scrollable, which means that it is possible to add scrolls to the composite widgets by using the SWT.H_SCROLL and SWT.V_SCROLL constants.

8.3.1. Table widget

A Table widget can display a set of String items or Images. In contrast to other composite widgets, it is not possible to add composite controls to the table widget. A sub component of a table widget must be of the TableItem type.

Figure 11. Table widget.

The constants in Table 7 can be used with the table widget.

Table 7. SWT Table style bit constants
SWT.MULTI
SWT.SINGLE
Enables a single or multi selection
SWT.FULL_SELECTION Enables full row selection
SWT.CHECK Displays a check box at the beginning of each row

The code snippet in Source 8 shows a table widget usage containing two columns.

Source 8. Table widget example
  final Table table =    new Table(shell,SWT.SINGLE);  TableColumn col1  =    new TableColumn(table,SWT.LEFT);  col1.setText("Coloumn 1");  col1.setWidth(80);  TableColumn col2  =    new TableColumn(table,SWT.LEFT);  col2.setText("Coloumn 2");  col2.setWidth(80);  TableItem item1 = new TableItem(table,0);  item1.setText(new String[]{"a","b"});  TableItem item2 = new TableItem(table,0);  item2.setText(new String[]{"a","b"});  table.setHeaderVisible(true);  table.setLinesVisible(true);
8.3.2. Combo widget

A Combo widget allows users to select a value from a list of values or optionally enter a new value. The combo widget is similar to the List widget, but it uses a limited space.

Although the combo widget is a composite, it does not make sense to add child elements to it. Its elements must be of the String type. An element to a combo widget can be added by using the add(String element) method defined in the combo class.

     

Figure 12. Combo boxes in different styles.

The following SWT constants can be used with the Combo widget.

Table 8. SWT Combo style bit constants
SWT.DROP_DOWN Drop-down combo box
SWT.READ_ONLY Read-only combo box
SWT.SIMPLE Simple combo box (not drop-down combo box). See Figure 11.

The following example shows a Combo widget's usage.

Source 9. Combo example
 final Combo combo =   new Combo(shell,SWT.DROP_DOWN); for (int i = 1; i < 11; i++) {   combo.add(i+".) element "); } combo.setText("Text"); combo.addSelectionListener(  new SelectionAdapter()  {    public void widgetSelected(SelectionEvent e)    {      System.out.println("Selection:"+                         combo.getText());    }  } );
8.3.3. Tree widget

A Tree widget represents a selectable hierarchy of items in a tree. Altough the Three class is a composite, it is not allowed to add composite classes to the Three class. Sub items of a Tree class must be of the ThreeItem type.

     

Figure 13. Tree widgets in different styles.

The following table shows a list of Tree widget constants.

Table 9. SWT Combo style bit constants
SWT.SINGLE
SWT.MULTI
Allows single or multiple selections
SWT.CHECK shows a check box at the begining of each node

A simple Tree widget example is shown below.

Source 10. Tree example
 final Tree tree =   new Tree(shell,SWT.SINGLE); for (int i = 1; i < 11; i++) {   final TreeItem item1  =    new TreeItem(tree,SWT.NULL);   item1.setText("node "+i);   for (int j = 1; j < 6; j++)   {     final TreeItem item11 =       new TreeItem(item1,SWT.NULL);     item11.setText("node "+i+"."+j);   } } tree.addSelectionListener(  new SelectionAdapter()  {   public void widgetSelected(SelectionEvent e)   {     System.out.println("Selection:"+                   tree.getSelection()[0]);   }  } );




Page 5 of 6



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel