In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
JComboBox is a component that combines a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value.
Java Swing ComboBox Example
In the below example, we have a combo box and a label. The combo box contains a list of strings denoting the names of Linux distributions. The selected item from the combo box is displayed in the label. The combo box uses its ItemListener to detect changes.
package net.sourcecodeexamples.swingexample.components2;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static javax.swing.GroupLayout.Alignment.BASELINE;
public class ComboBoxExample extends JFrame
implements ItemListener {
private static final long serialVersionUID = 1L;
private JLabel display;
private JComboBox < String > box;
private String[] distros;
private void initializeUI() {
distros = new String[] {
"Ubuntu",
"Redhat",
"Arch",
"Debian",
"Mint"
};
box = new JComboBox < > (distros);
box.addItemListener(this);
display = new JLabel("Ubuntu");
createLayout(box, display);
setTitle("JComboBox");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void createLayout(JComponent...arg) {
JPanel pane = (JPanel) getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
gl.setVerticalGroup(gl.createParallelGroup(BASELINE)
.addComponent(arg[0])
.addComponent(arg[1])
);
pack();
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
display.setText(e.getItem().toString());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ComboBoxExample comboBoxExample = new ComboBoxExample();
comboBoxExample.initializeUI();
comboBoxExample.setVisible(true);
});
}
}
Let's understand the above Java program.
The JComboBox will hold these string array: ```java` distros = new String[] {"Ubuntu", "Redhat", "Arch", "Debian", "Mint"};
The display area is a simple JLabel. It displays the item that is initially shown in the combo box:
```java
display = new JLabel("Ubuntu");
The constructor of the JComboBox takes a string array of Linux distributions. We plug a listener to the created object:
box = new JComboBox<>(distros);
box.addItemListener(this);
Vertically, the two components will be aligned to the baseline of their text:
gl.setVerticalGroup(gl.createParallelGroup(BASELINE)
.addComponent(arg[0])
.addComponent(arg[1])
);
The itemStateChanged() is invoked when an item has been selected or deselected by the user. We check for ItemEvent.SELECTED state and set the combo box's selected item to the label.
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
display.setText(e.getItem().toString());
}
}
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment