All About Class List in AWT in Java

0
104
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

All About Class List in AWT in Java
All About Class List in AWT in Java

 

All About Class List in AWT in Java:

List class provides a compact multi choice,scrolling selection list.The list can be set up so that the user can pick one item or multiple items from it.It can be noted that Choice object shows only the single selected item menu,whereas List object can be constructed to show any number of choices in the visible window.A listbox is a vertical list of single lines of texts.We can add to it,select or change items and delete items.If we add more items than it can be displayed,a scrollbar appears on the right side.We can select an item by clicking on the item and clicking on an item that is not selected and automatically it will deselect any previously selected item.When an item is clicked and get selected,AWT send a List select event to the scrolling list.When an item is clicked and gets deselected,AWT sends a List deselect event to the scrolling list.When the user double clicks on an item in a scrolling list,AWT sends an action event to the scrolling list after the list select or deselect event.The event’s target is the scrolling list and its object is the String label of the item selected or deselected in the scrolling list.If an application wants to perform some action,based on an item being selected or deselected,it must override the handleEvent() method of the scrolling list or any of its containing windows.

READ  Dr Java - A Uncomplicated IDE For Beginners

The structure of the class  List is given as:

 


public class java.awt.List extends java.awt.Component{
//constructors
new List();//default constructor to create a default list with no items and no selection
new List(int numberOfItemsToDisplay,boolean multiSelectionChoice);
//creates a new scrolling list initialized to display the specified number of rows.If the multiple
//selection agrument is true,then the user can select multiple items at a time from the list. If 
//it is false,only one item at a time can be selected.
//methods
public void addItem(String item);//adds the item in the list
public void addItem(String item,int n);//adds the item in the list at nth position
public void addNotify();
public boolean allowsMultipleSelection();
public void clear();
public int countItem();
public void delItem(int position);
public void delItems(int start,int end);
public void removeAll();//clears the list
public void remove(int n);//removes item at nth position.
public void remove(String item);//removes first item that matches the String item.
public void makeVisible(int index);
public void removeNotify();
public int getItemCount();//returns number of elements in the list.
public void deselect(int n);//deselect element at nth position.
public int getSelectedIndex();//returns the index of the selected item.
public int getVisibleIndex();
public int[] getSelectedIndexes();//returns the indexes of the selected items.
public int getRows();
public String getSelectedItem();//returns the text of the selected item.
public String[] getSelectedItems();//returns the text array of the selected items.
public String getItem(int n);//returns the text of item n.
public boolean isSelected(int n);//returns true if the n item is selected,false otherwise.
public void select(int n);//Selects the item at n.
public void repalceItem(int n,String newItem);//replaces item n with next text.
public void setMultipleSelection(Boolean value);//sets list to allow or not allow multiple selection.
public Dimension minimumSize();
public Dimension minimumSize(int rows);
public Dimension preferredSize();
public Dimension preferredSize(int rows);
protected String paramString();
}

We can also see the changes of the list if some program display element When user selects a line in a list box,if we call the addItemListener method,it causes the itemStateChanged method to be called whenever we click on a line in a listBox. Calling addActionListener() method causes the actionPerformed()method to be called whenever we double click on a listBox element.
Example


List os=new List(3,false);//single choice behaves like a choice.Mutually exclusive selection
os.addItem("MAC");
os.addItem("Solarics");
os.addItem("Windows");
List os=new List(3,true);//Multiple selection enabled
os.addItem("MAC");
os.addItem("Solarics");
os.addItem("Windows");
 
Don't miss out!
Subscribe To Newsletter
Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here