Home Tutorials Swt Create Popup List in SWT



Create Popup List in SWT
Posted on: October 3, 2008 at 12:00 AM
SWT allows to create the popup list by providing the class PopupList of package org.eclipse.swt.custom. In the given example, we are going to popup the list of Indian states.

Create Popup List in SWT

     

In this section, you will learn how to create the popup list.

SWT allows to create the popup list by providing the class PopupList of package org.eclipse.swt.custom. In the given example, we are going to popup the list of Indian states. For this, we have create an array of states. The class PopupList create the list. The method setItem() adds the items to the list. The method list.open(shell.getBounds()) opens the list and get the selected item. The Button class is used to create the button to perform an action by calling the class SelectionEvent.

To display the selected state, we have used the class Text which creates the textBox with the button. As the button is clicked, the list will be displayed. On selecting the state, the method text.setText("Selected:"+selected) sets the selected state on the textBox. The method text.setSize() sets the size of textBox.

Here is the code of PopupListExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class PopupListExample {
  Text text;
  String[] states = {"Andra Pradesh","Arunachal Pradesh",
  "Assam","Bihar","Chhattisgarh","Goa","Gujarat","Haryana",
  "Himachal Pradesh","Jammu and Kashmir","Jharkhand",
  "Karnataka","Kerala","Madya Pradesh","Maharashtra","Manipur",
  "Meghalaya","Mizoram","Nagaland","Orissa","Punjab","Rajasthan",
  "Sikkim","Tamil Nadu","Tripura","Uttaranchal","Uttar Pradesh",
  "West Bengal"};

  public void init() {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("PopupList");
  createList(shell);
  shell.pack();
  shell.setSize(230,50);
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  display.sleep();
  }
  }
  display.dispose();
  }
  private void createList(final Shell shell) {
  shell.setLayout(new RowLayout());
  Button button = new Button(shell, SWT.PUSH);
  button.setText("click");
  button.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  PopupList list = new PopupList(shell);
  list.setItems(states);
  String selected = list.open(shell.getBounds());
  if(selected==null){
  System.out.println("You haven't selected any state");
  }
  else{
  text.setText("Selected:"+selected);
  text.setSize(180,20);
  }
  }
  });
  text = new Text(shell, SWT.BORDER | SWT.SINGLE);
  text.setEditable(true);
  }
  public static void main(String[] args) {
  new PopupListExample().init();
  

Output will be displayed as:

On clicking the button, list will be displayed:

On selecting the state, output will be displayed as:

Download Source Code

     

Related Tags for Create Popup List in SWT:
ceclipsearrayshellclasslistormselectformbuttoneventiocustompopupswtmethodsedgetipvistateselectionclithisidactionpackagesetopencreateplistforexamplecallcteaddtoexamselectedwssheititemitemscallingliusepepopinrmcalasstamntpscaddpackadstatesclesememsallagepenmeproackxaxampsclipboundsatpackishallivmplraygodiaeaandarstatactwtttsstbothswavstelectioneclorgpleplprndonomo


More Tutorials from this section

Ask Questions?    Discuss: Create Popup List in SWT  

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.