Home Tutorials Swt How to create Combo Box in SWT



How to create Combo Box in SWT
Posted on: October 3, 2008 at 12:00 AM
This section illustrates you how to create a combo box.

How to create Combo Box in SWT

     

This section illustrates you how to create a combo box.

In this example, we have created two different combo boxes where one is performing an action on the second combo box. The items tea , coffee and cold drink are added to the  first combo box by using the method add() of Combo class.  To perform an action on the second combo box, the class SelectionEvent is called. If you select the cold drink from the first combo box, the array of drinks is added to the second combo box by using the method combo2.setItems(drinks).The method getText().equals("Cold drink") returns the text Cold drink. If you select Tea or Coffee, the second combo box shows the text 'not Applicable'.

 

 

Here is the code of ComboBox.java

import java.util.Arrays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.GridLayout;

public class ComboBox {
  Display display = new Display();
  Shell shell = new Shell(display);

  public ComboBox() {
  shell.setText("Combo box");
  shell.setLayout(new GridLayout(4false));

  Label label=new Label(shell, SWT.NULL);
  label.setText("What do you like to drink?");
  final Combo combo1 = new Combo(shell, SWT.VERTICAL |
   SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);

  final Combo combo2 = new Combo(shell, SWT.VERTICAL| 
   SWT.BORDER  |SWT.READ_ONLY);

  
 combo1.add("Tea");
 combo1.add("Coffee");
 combo1.add("Cold drink");
 combo1.addSelectionListener(new SelectionAdapter() {
 public void widgetSelected(SelectionEvent e) {
  if (combo1.getText().equals("Cold drink")) {
  String[] drinks = new String[]{"Pepsi""CocaCola"
   "Miranda"
,"Sprite","ThumbsUp"};
  combo2.setItems(drinks);
  combo2.setEnabled(true);
  combo2.add("-Select-");
  combo2.setText("-Select-");
  else if (combo1.getText().equals("Tea")) {
  combo2.add("Not Applicable");
  combo2.setText("Not Applicable");
  else {
  combo2.add("Not Applicable");
  combo2.setText("Not Applicable");
  }
  }
  });
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  
  display.sleep();
  }
  }
  display.dispose();
  }
  public static void main(String[] args) {
  new ComboBox();
  }
}

Output will be displayed as:

As no value is added for the item tea and coffee, therefore if you select the tea, it will show you Not Applicable on the second combo box.

If you select the cold drink, it will show you select.

After selecting the cold drink, output will be displayed as:

Download source Code:

     

Related Tags for How to create Combo Box in SWT:
ccomclassormformdiffiomethodrmiusingthisactioncreateifforexampleaddboxwheretoexamldeilititemitemssectionpefirstddeinrmdifferentcomboasmnttrminddminadclesememsmehowrateratescoffxaxampsxecreatedeecoffeecolataddedoldkisirinkhallmpleaandaractcoldstrtwssrirenteathcondavstfepleplmindonombomolo


More Tutorials from this section

Ask Questions?    Discuss: How to create Combo Box in SWT   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.