g. Develop swing application to present set of choices for a user to select stationary product and display the prize of product after selection from list

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:

Programs based on AWT and Swing.

Programs based on AWT and Swing.





Post a Comment

0 Comments