Java Program to Create a List Box to Select Multiple Items and Display it in the Frame

«
»

This is a Java program to create a list box to select multiple items and display it in the frame.

Problem Description

We have to write a program in Java such that it creates a list box (C, C++, Java, Python and R) from which the user can select one or multiple items, and the selected items are displayed in the frame.

Expected Input and Output

For displaying the items selected from a list box, we can have the following sets of input and output.
Consider the languages C, C++, Java, Python and R are in the list box.

advertisement

1. To display the list box:

If the program is executed,
then it is expected that a list box is displayed with the below languages.
C, C++, Java, Python and R

2. To select one item:

If any one item, say Java is selected from the list (C, C++, Java, Python and R),
then the expected output is "Items Selected : Java".

3. To select three items:

advertisement
If any three items, say C++, Java, R are selected from the list (C, C++, Java, Python and R),
then the expected output is "Items Selected : C++, Java, R".

4. To select all items:

If all items, say C, C++, Java, Python, R are selected from the list (C, C++, Java, Python and R),
then the expected output is "Items Selected : C, C++, Java, Python, R".
Problem Solution

1. Create a frame and a list of type String with the languages, and a label.
2. Add ListSelectionListener to the list. The ListSelectionListener defines method public void valueChanged(ListSelectionEvent).
3. Get the array of indices selected and get the corresponding items.
4. Display the items selected.

advertisement
Program/Source Code

Here is source code of the Java Program to display the items selected from a list box. The program is successfully compiled and tested using javac compiler on Fedora 30.

  1. /*Java Program to display items selected from a list box*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import javax.swing.event.*;
  5. class List_Box implements ListSelectionListener
  6. {
  7.     static JList<String> list;
  8.     static JLabel label;
  9.     //Driver function
  10.     public static void main(String args[])
  11.     {
  12.     	//Create a frame
  13. 	JFrame frame=new JFrame("List Box");
  14. 	frame.setSize(500,500);
  15. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 	frame.setLayout(null);
  17. 	frame.getContentPane().setBackground(Color.white);
  18. 	//Create a list
  19. 	String lang[]={"C","C++","Java","Python","R"};
  20. 	list=new JList<String>(lang);
  21. 	list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  22. 	list.setBounds(150,100,150,150);
  23. 	frame.add(list);
  24. 	//Create a label
  25. 	label=new JLabel("Select items from list");
  26. 	label.setBounds(0,0,500,50);
  27. 	frame.add(label);
  28. 	//Create an object
  29. 	List_Box obj=new List_Box();
  30. 	//Add ListSelectionListener to list
  31.         list.addListSelectionListener(obj);
  32. 	//Display the frame
  33.         frame.setVisible(true);
  34.     }
  35.     //Function to display the items selected
  36.     public void valueChanged(ListSelectionEvent e)
  37.     {
  38. 	//Get index of items selected
  39. 	int index[]=list.getSelectedIndices();
  40. 	//Get the items selected from their indices
  41. 	String str="";
  42. 	for(int i=0;i<index.length;i++)
  43. 		str=str+list.getModel().getElementAt(index[i])+", ";
  44. 	        str=str.replaceAll(", $", "");
  45. 	//Change label to items selected
  46. 	label.setText("Items Selected : "+str);
  47.     }
  48. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create a list using String lang[] and add it to the frame by using frame.add();.
String lang[]={“C”,”C++”,”Java”,”Python”,”R”}; –> array of languages (C, C++, Java, Python and R) is added to the list.
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); –> It allows user to select multiple items in the list.
list.setBounds(150,100,150,150); –> Sets the position of list.
frame.add(list); –> It will add the list to the frame.

4. list=new JList(lang); –> A List of type string is created using JList<E> where E is the type of data with the list will have.
5. Create a label with a setBounds() values and add it to the frame. This label is used for output to display a selected items from the list. By default label name will be Select items from list.
6. List_Box obj=new List_Box(); is used to create the object. Once object is created associate ListSelectionListener with the list.
7. ListSelectionListener of the javax.swing.event package is associated with the list created.
8. In method valueChanged the indices of selected items is obtained using function getSelectedIndices(), which returns an array of integers.
9. The item corresponding to the index is obtained using function getElementAt(int), which returns the item name.
10. label.setText(“Items Selected : “+str); –> It will change the lable name from Select items from list to Items Selected : and also displays the selected items from the list.

Runtime Test Cases

Here’s the run time test case for displaying items selected from a list.

advertisement

Test case 1 – Here’s the runtime output to view the list. When the program is executed, then it displays a list box with the following languages (C, C++, Java, Python and R).


Test case 2 – Here’s the runtime output to select any one item. For example, if the language Java is selected from the list (C, C++, Java, Python, R), then the program displays “Items Selected : Java”.


Test case 3 – Here’s the runtime output to select any three items. For example, if the language C++, Java, R is selected from the list (C, C++, Java, Python, R), then the program displays “Items Selected : C++, Java, R”.


Test case 4 – Here’s the runtime output to select all items. For example, if all the languages are selected from the list (C, C++, Java, Python, R), then the program displays “Items Selected : C, C++, Java, Python, R”.


Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn