CODE:
package swing_practicals;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class List1 extends JFrame implements ListSelectionListener {
JList lst1;
Color clr[]={Color.RED,Color.GREEN,Color.BLUE,Color.BLACK,Color.CYAN,Color.PINK};
List1()
{
setTitle("ListBox Demo");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
String str[]={"Red","Green","Blue","Black","Cyan","Pink"};
lst1 = new JList(str);
lst1.setVisibleRowCount(4);
lst1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane sp = new JScrollPane(lst1);
add(sp);
getContentPane().setBackground(Color.RED);
lst1.addListSelectionListener(this);
}
public void valueChanged(ListSelectionEvent e)
{
getContentPane().setBackground(clr[lst1.getSelectedIndex()]);
}
public static void main(String args[])
{
new List1();
}
}
output:
0 Comments