JList and List Models - potatoscript/JavaSwing GitHub Wiki
JList
is a Swing component that allows you to display a list of items from which users can select one or more items. It's ideal for displaying lists where the user needs to pick from multiple options.
- Single or multiple selections: You can allow the user to select one or multiple items from the list.
- Flexible rendering: You can customize how the items in the list are displayed.
- Interaction: Users can click on items to select them.
import javax.swing.*;
public class JListExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JList Example");
// Create a list of items
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Create JList with the list of items
JList<String> list = new JList<>(fruits);
// Add JList to JScrollPane to add scrolling capability
JScrollPane scrollPane = new JScrollPane(list);
// Set layout and add JScrollPane to frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(scrollPane);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
- This is the list of items that will be displayed in the JList.
✅ JList list = new JList<>(fruits);
- Creates a new JList with the items we defined.
✅ JScrollPane scrollPane = new JScrollPane(list);
- Adds a scroll bar to the JList, which allows you to scroll through the list of items if they are too many to fit on the screen.
+-------------------------------+
| ▼ Apple |
| Banana |
| Cherry |
| Date |
| Elderberry |
+-------------------------------+
You can handle the selection event of the JList to get the selected item(s) when the user clicks on them.
import javax.swing.*;
import java.awt.event.*;
public class JListSelection {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JList Selection");
// Create list of items
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Create JList
JList<String> list = new JList<>(fruits);
// Create JLabel to display selected item
JLabel label = new JLabel("Selected: None");
// Add ListSelectionListener to handle selection events
list.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
String selectedValue = list.getSelectedValue();
label.setText("Selected: " + selectedValue);
}
});
// Add JList and JLabel to the frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(new JScrollPane(list));
frame.add(label);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ list.addListSelectionListener(e -> {...});
- This adds a listener to the JList, so when a user selects an item, it triggers the event and updates the label with the selected value.
+-------------------------------+
| ▼ Apple |
| |
| Selected: None |
+-------------------------------+
(When Banana is selected):
+-------------------------------+
| ▼ Banana |
| |
| Selected: Banana |
+-------------------------------+
By default, JList allows for single selection. However, you can change it to allow multiple selections.
import javax.swing.*;
public class MultipleSelectionJList {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Multiple Selection JList");
// Create list of items
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Create JList and set to multiple selection mode
JList<String> list = new JList<>(fruits);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Create JScrollPane for list
JScrollPane scrollPane = new JScrollPane(list);
// Set layout and add components to frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(scrollPane);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
- This allows multiple items to be selected at once.
+-------------------------------+
| [ ] Apple |
| [ ] Banana |
| [ ] Cherry |
| [ ] Date |
| [ ] Elderberry |
+-------------------------------+
ListModel
provides a way to manage the data behind a JList. It separates the data from the UI, making the list more flexible and easier to update. DefaultListModel
is the default implementation.
import javax.swing.*;
import javax.swing.event.*;
public class JListWithListModel {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JList with ListModel");
// Create DefaultListModel and add items
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement("Apple");
listModel.addElement("Banana");
listModel.addElement("Cherry");
// Create JList with ListModel
JList<String> list = new JList<>(listModel);
// Add ListSelectionListener to handle selection events
list.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
String selectedValue = list.getSelectedValue();
System.out.println("Selected: " + selectedValue);
}
});
// Add JList to JScrollPane and frame
JScrollPane scrollPane = new JScrollPane(list);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(scrollPane);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ DefaultListModel listModel = new DefaultListModel<>();
- This creates a
DefaultListModel
, which allows you to add and remove elements dynamically.
✅ JList list = new JList<>(listModel);
- This creates a JList that uses the
listModel
to display items.
- The program will display the list, and you will see the selected item printed in the console when you select it.