source file name: ListSelectEvent2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ListSelectEvent2 extends JFrame implements ListSelectionListener
{
JList ls;
Object ct;
JLabel lbtxt;
public ListSelectEvent2()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
JLabel lb=new JLabel("select the color :");
lbtxt=new JLabel();
String cols[]= { "blue","green","yellow",
"pink","red"};
ls=new JList(cols);
JScrollPane sp=new JScrollPane(ls);
add(lb);
add(sp);
ls.addListSelectionListener(this);
add(lbtxt);
}
public void valueChanged(ListSelectionEvent lse)
{
String cls=ls.getSelectedValue().toString();
Color col=null;
System.out.println("selected day "+ls.getSelectedValue());
if(cls.equals("blue"))
col=Color.blue;
if(cls.equals("pink"))
col=Color.pink;
if(cls.equals("red"))
col=Color.red;
if(cls.equals("green"))
col=Color.green;
if(cls.equals("yellow"))
col=Color.yellow;
ls.setBackground(col);
}
public static void main(String as[])
{
ListSelectEvent2 f=new ListSelectEvent2();
f.setTitle("list selection event program");
f.setSize(300,300);
f.setVisible(true);
}
}
output:
0 Comments
Post a Comment