JList is a Swing component that displays a list of items in a vertical list. It allows users to select one or more items from the list. It is commonly used together with a JScrollPane to handle scrolling when the list contains many items.
Example:
Source Code:
public List() {
initComponents();
jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) { // avoid double events
java.util.List selected = jList1.getSelectedValuesList();
String text = String.join(", ", selected); // joins items with comma and space
lblResult.setText("Selected: " + text);
}
}
});
}
Result:
Watch the video:

