Friday, July 26, 2013

List

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:
-List()
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.
-getItemCount()
get the number of text items in the list.
-getItems()
gets all text items in the list.
-getSelectedItem()
gets the selected text item.
-getSelectedItems()
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.
-removeAll()
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){
setTitle(title);
setSize(new Dimension(500,300));
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
List lst=new List(15,false);
lst.addItemListener(this);
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fs=ge.getAllFonts();
for(Font f:fs){
lst.add(f.getName());
}
lbl=new Label();
add(lst);
add(lbl);
validate();
}
public void itemStateChanged(ItemEvent e){
List l=(List)e.getSource();
String text=l.getSelectedItem();
lbl.setText(text);
validate();
}

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

List awt

No comments:

Post a Comment