1. Create GUI to present a set of choices for a user to select stationary products and display the price of Product after selection from the list

ListSelectionListener is used to react to the change in selection of the value in JList Component

import javax.swing.*;

import java.awt.*;

import javax.swing.event.*;

class ListDemo extends JFrame implements ListSelectionListener

{

Container cp;

JList jlist;

JLabel lblPrice;

String[] prod={“Pen”,”Pencil”,”Ruler”,”Eraser”,”Shapner”};

int[] price={12,13,15,45,25};

ListDemo ()

{

cp=this.getContentPane();

cp.setLayout(new FlowLayout());

jlist =new JList(items);

lblPrice=new JLabel(“”);

jlist.addListSelectionListener(this);

cp.add(jlist);

cp.add(lblPrice);

}



public void valueChanged(ListSelectionEvent e)

{

if(jlist.getSelectedIndex()==-1)

{

lblPrice.setText(“no items selected”);

}

else

{

int index= jlist.getSelectedIndex();

lblPrice.setText(“Price: “+price[index]);

}

}

public static void main(String args[])

{

ListDemo lstObj= new ListDemo();

lstObj.setSize(400,400);

lstObj.setVisible(true);

lstObj.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}