More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Join and Excel yourself with our Online instructor led training sessions
Training Courses
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.