Home Tutorials Swt Creating List in Java using SWT



Creating List in Java using SWT
Posted on: October 4, 2008 at 12:00 AM
This section illustrates you how to create a list of certain items using the Standard Widget Toolkit

Creating List in Java using SWT

     

This section illustrates you how to create a list of certain items using the Standard Widget Toolkit

In this example, we have create a list of certain items using SWT that provides the class List of package org.eclipse.swt.widgets to create the list in Java language.

The style SWT.MULTI provided by the List class is used to select multiple items from the list.  The method list.add() adds the array to the list and the method list.getSelectionIndex() returns the index of selected item. Go through the below given example code that illustrates, how to create list of items in Java.

 Here is the code of ListExample.java

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

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

  public ListExample() {
  RowLayout rowLayout = new RowLayout();
  shell.setLayout(rowLayout);
  shell.setText("List");
  (new Label(shell, SWT.NULL)).setText("Which game you like most? ");
  
  final List list = new List(shell, SWT.MULTI |  SWT.V_SCROLL);
  final Text text = new Text(shell,  SWT.BORDER);
  String[] sports = new String[]{"Chess""Cricket""FootBall",
     "Lawn Tennis","Badminton","Hockey","BasketBall","Golf","Table 
  Tennis"
,"VolleyBall"};
  
  for(int i=0; i<sports.length; i++)
  list.add(sports[i]);
  list.addSelectionListener(new SelectionListener() {
  
  public void widgetSelected(SelectionEvent e) {
  System.err.println(list.getSelectionIndex());
  int[] indices = list.getSelectionIndices();
  String[] items = list.getSelection();
  StringBuffer buffer = new StringBuffer(" ");
  for(int i=0; i < indices.length; i++) {
  buffer.append(items[i]);
  if(i == indices.length-1)
  buffer.append('.');
  else
  buffer.append(", ");
  }
  System.out.println(buffer.toString());
  text.setText(buffer.toString());
  }

  public void widgetDefaultSelected(SelectionEvent e) {
  int[] indices = list.getSelectionIndices();
  String[] items = list.getSelection();
  StringBuffer buffer = new StringBuffer(" ");
  for(int i=0; i < indices.length; i++) {
  buffer.append(items[i]);
  if(i == indices.length-1)
  buffer.append('.');
  else
  buffer.append(", ");
  }
  System.out.println(buffer.toString());
  text.setText(buffer.toString());
  }
  });
  shell.pack();
  shell.setSize(350,180);
  shell.open();
  
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  display.sleep();
  }
  }
  display.dispose();
  }
  public static void main(String[] args) {
  new ListExample();
  }
}

Output will be displayed as:

Download Source Code

     

Related Tags for Creating List in Java using SWT:
javaceclipseideclasslistlanguageioswtgetipwidgetsviwidgetusingclithisidtoolaipackageoocreatetoolkitexampletostandardexamlaneilitdesitemitemssectionliceinasstampstrjndapackclesememsagehowprorateratesackxaxampsclipatpackishallkitmplgetseaandarstrrtwidwtvassrdthswceravsthateclorgpleplprndonolo


More Tutorials from this section

Ask Questions?    Discuss: Creating List in Java using 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.