// java Program to create a simple JList
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class ListDemo extends JFrame implements ListSelectionListener
{
//lists
static JList ls;
JLabel lbtxt;
//main class
public static void main(String[] args)
{
ListDemo f=new ListDemo();
f.setTitle("SwingListProgram");
f.setSize(400,400);
f.show();
}
public ListDemo()
{
JLabel lb= new JLabel("select the day of the week");
lbtxt= new JLabel("");
setLayout(null);
//String array to store weekdays
String week[]= { "Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};
//create list
ls= new JList(week);
lb.setBounds(50,50,150,30);
ls.setBounds(200,50,100,150);
lbtxt.setBounds(50,200,150,30);
add(lb);
add(ls);
add(lbtxt);
ls.addListSelectionListener(this);
}
public void valueChanged(ListSelectionEvent lse)
{
System.out.println("selected day: "+ls.getSelectedValue());
lbtxt.setText("selected day: "+ls.getSelectedValue());
}
}
output:
0 Comments
Post a Comment