Friday, July 26, 2013


List is a component that allows you to create a scrolling list of text items. You can configure the list for single or multiple selection. The List can generate item event. To receive the event, the application must implement the ItemListener and register the listener.

Constructors of the List class:
creates a new scrolling list.
-List(int rows)
creates a new scrolling list with the number of visible rows.
-List(int rows, boolean multipleMode)
creates a new scrolling list with the number of visible rows and selection mode.

Useful methods of the List class:
-add(String text)
adds text item to the end of the list.
-add(String text,int index)
adds text item to the list at the specified position.
-deselect(int index)
delects the text item at the specified index.
-getItem(int index)
gets text item at the specified index.
get the number of text items in the list.
gets all text items in the list.
gets the selected text item.
get all selected text items.
-isIndexSelected(int index)
determines whether or not the text item at the specified index is selected. 
-remove(int index)
removes the text item at the specified index.
-remove(String item)
removes the text item that matches the item argument.
removes all text items of the list.
-select(int index)
select the text item at the specified index.
-setMultipleMode(boolean multipleMode)
sets selection mode of the list to false or true. If false, only a single text item can be selected. 

In the example program below, we construct a List component to display 15 rows of items with single item selection. If the number of items added to the List beyond this rows limit, the scroll bars are automatically attached to the list for you.  The getAllFonts method of the GraphicsEnvironment class is used to retrieve all font objects from the system. All font objects are added to the List component by using its add method.

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame implements ItemListener{
Label lbl;
InterfaceShow(String title){
setSize(new Dimension(500,300));
setLayout(new FlowLayout(FlowLayout.LEFT));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
List lst=new List(15,false);
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fs=ge.getAllFonts();
for(Font f:fs){
lbl=new Label();
public void itemStateChanged(ItemEvent e){
List l=(List)e.getSource();
String text=l.getSelectedItem();

public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using List");

List awt

No comments:

Post a Comment