CODE:
package swing_prcaticals;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class List2 extends JFrame implements ListSelectionListener
{
JList lst1;
String price[]={"Rs.10/-","Rs.5/-","Rs.50/-","Rs.2/-"};
List2()
{
setTitle("ListBox Demo");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new FlowLayout());
String str[]={"Pen","Pencil","Crayon Box","Rubber"};
lst1=new JList(str);
lst1.setVisibleRowCount(4);
lst1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane sp = new JScrollPane(lst1);
add(sp);
lst1.addListSelectionListener(this);
}
public void valueChanged(ListSelectionEvent e)
{
String item_name=lst1.getSelectedValue().toString();
JOptionPane.showMessageDialog(this,"The price of "+item_name+" is "+price[lst1.getSelectedIndex()],"Welcome",2);
//setBackground(Color.Red);
}
public static void main(String args[])
{
new List2();
}
}
OUTPUT:
0 Comments