This is a Java program to create a list box to select multiple items and display it in the frame.
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.
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.
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:
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".
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.
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.
/*Java Program to display items selected from a list box*/
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class List_Box implements ListSelectionListener
{
static JList<String> list;
static JLabel label;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("List Box");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.white);
//Create a list
String lang[]={"C","C++","Java","Python","R"};
list=new JList<String>(lang);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setBounds(150,100,150,150);
frame.add(list);
//Create a label
label=new JLabel("Select items from list");
label.setBounds(0,0,500,50);
frame.add(label);
//Create an object
List_Box obj=new List_Box();
//Add ListSelectionListener to list
list.addListSelectionListener(obj);
//Display the frame
frame.setVisible(true);
}
//Function to display the items selected
public void valueChanged(ListSelectionEvent e)
{
//Get index of items selected
int index[]=list.getSelectedIndices();
//Get the items selected from their indices
String str="";
for(int i=0;i<index.length;i++)
str=str+list.getModel().getElementAt(index[i])+", ";
str=str.replaceAll(", $", "");
//Change label to items selected
label.setText("Items Selected : "+str);
}
}
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
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.
Here’s the run time test case for displaying items selected from a list.
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.
If you find any mistake above, kindly email to [email protected]- Apply for Java Internship
- Practice BCA MCQs
- Practice Programming MCQs
- Check Java Books
- Apply for Computer Science Internship