Home Tutorials Swt Creating List in Java using SWT

Related Tutorials


 
 

Share on Google+Share on Google+

Creating List in Java using SWT

Advertisement
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

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 4, 2008

Related Tutorials

Discuss: Creating List in Java using SWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
ERVIN
March 31, 2012
USER INPUT BOX INTEGER TO ARRAYLIST

CAN YOU PROVIDE THIS BEST REGARDS ERVIN
DMCA.com