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

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

                         

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.