Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to create a Java, that will allows the user to choose multiple products/ items.

+4 votes
asked Jan 28, 2021 by BrianViloria115 (150 points)

1 Answer

0 votes
answered Jan 28, 2021 by Purushottam Kumar (240 points)
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);

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...