JComboBox and Dropdowns - potatoscript/JavaSwing GitHub Wiki
JComboBox
is a component in Java Swing that allows users to select an option from a dropdown list. It displays a list of items that the user can choose from, and the user can either select an item from the list or type their own choice (if the combo box is editable).
- Dropdown list: Displays a list of options for users to choose from.
- Editable: Users can type their own value (if configured as editable).
- Single selection: Only one item can be selected at a time.
import javax.swing.*;
public class JComboBoxExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JComboBox Example");
// Create JComboBox with some items
String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
JComboBox<String> comboBox = new JComboBox<>(items);
// Set layout and add JComboBox to frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(comboBox);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
- This creates an array of options that will be displayed in the dropdown list.
✅ JComboBox comboBox = new JComboBox<>(items);
- This creates a combo box with the specified items.
+-------------------------------+
| ▼ Apple |
| |
| |
+-------------------------------+
You can handle the selection event when the user selects an item from the dropdown using ActionListener.
import javax.swing.*;
import java.awt.event.*;
public class JComboBoxSelection {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JComboBox Selection");
// Create JComboBox with some items
String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
JComboBox<String> comboBox = new JComboBox<>(items);
// JLabel to display the selected item
JLabel label = new JLabel("Selected: None");
// Add ActionListener to update label when selection changes
comboBox.addActionListener(e -> label.setText("Selected: " + comboBox.getSelectedItem()));
// Set layout and add components
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(comboBox);
frame.add(label);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ comboBox.addActionListener(e -> label.setText("Selected: " + comboBox.getSelectedItem()));
- When an item is selected from the combo box, the label text is updated to display the selected item.
+-------------------------------+
| ▼ Apple |
| |
| Selected: None |
+-------------------------------+
(When Banana is selected):
+-------------------------------+
| ▼ Banana |
| |
| Selected: Banana |
+-------------------------------+
You can make the combo box editable so users can type their own values.
import javax.swing.*;
public class EditableJComboBox {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Editable JComboBox");
// Create JComboBox with some items
String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
JComboBox<String> comboBox = new JComboBox<>(items);
// Make JComboBox editable
comboBox.setEditable(true);
// Set layout and add JComboBox to frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(comboBox);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ comboBox.setEditable(true);
- This allows the user to type their own text in the combo box, in addition to selecting from the list.
+-------------------------------+
| [Editable Text Field] |
| |
| |
+-------------------------------+
You can customize the appearance of the items in the combo box by using ListCellRenderer to modify how each item is displayed.
import javax.swing.*;
import java.awt.*;
public class CustomizedJComboBox {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Customized JComboBox");
// Create JComboBox with items
String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
JComboBox<String> comboBox = new JComboBox<>(items);
// Customize appearance with a custom renderer
comboBox.setRenderer(new ListCellRenderer<String>() {
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel(value);
if (isSelected) {
label.setBackground(Color.BLUE);
label.setForeground(Color.WHITE);
} else {
label.setBackground(Color.WHITE);
label.setForeground(Color.BLACK);
}
label.setOpaque(true);
return label;
}
});
// Set layout and add JComboBox to frame
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(comboBox);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ comboBox.setRenderer(new ListCellRenderer() {...});
- This customizes the appearance of each item in the combo box, changing the background and text colors when an item is selected.
- The combo box will display with a custom style, changing color when an item is selected.