List control in java

List

  • This class is useful to create a list box which is similer to choice menu
  • A list box presents the us with a scrolling list of text items
  • Unlike the choice object, the list object can be constructed to show any no. of choices in the visible window
  • It can also be created to allow mutliple selections
  • Constructors
    • List( )
    • List(int numRows)
    • List(int numRows, boolean multipleSelect)
  • Methods
    • void add(String name)
    • void add(String name, int index)
    • String getSelectedItem( )
    • int getSelectedIndex( )
    • String[ ] getSelectedItems( )
    • int[ ] getSelectedIndexes( )
    • int getItemCount( )
    • void select(int index)
    • String getItem(int index)
Example
import java.awt.*;
import java.awt.event.*;
class test extends Frame implements ItemListener
{
int[] msg;
List ls;
test()
{
setLayout(new FlowLayout());
ls = new List(4,true);
ls.add(“English”);
ls.add(“Spanish”);
ls.add(“Japanese”);
ls.add(“Chinese”);
ls.add(“Hindi”);
add(ls);
ls.addItemListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(“Selected Language: “,100,200);
msg = ls.getSelectedIndexes();
for(int i=0;i<msg.length;i++)
{
String item = ls.getItem(msg[i]);
g.drawString(item, 100, 220+i*20);
}
}
public static void main(String args[])
{
test t = new test();
t.setTitle(“My List Box”);
t.setSize(400, 400);
t.setVisible(true);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *