Java JComboBox
JComboBox inherits from class JComponent.The selected choice pop-ups at the top of the menu.
How to declare JCombox
[public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible]
JCombox constructors.
- JComboBox()-creates a combo box with default data model.
- JComboBox(Object [] items)-creates combo box that gets items from a specified array.
- JComboBox(Vector<?> items)-creates combo box with elements from a Vector
JCombox commonly used methods
- void addItem(Object object)-this method is used to add Item to the Item List.
- void removeItem(Object object)-used to remove item from Item List.
- void removeAllItems()-removes all items from the list.
- void setEditable(boolean )-if its set true,the JComboBox is editable.
- void addActionListener(ActionListener l)-adds actionListener
- void addItemListener(ItemListener i)-adds ItemListener.
Java JComboBox Example
[
import javax.swing.JComboBox; import javax.swing.JFrame; public class JComboBoxExample extends JFrame { JComboBoxExample() { super("JComboBox Example"); String languages []={"Java","C++","PHP","JavaScript","C#"}; JComboBox box=new JComboBox(languages); box.setBounds(50, 50,90,20); add(box); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JComboBoxExample(); } }
]
Java JComboBox with ActionListener Example
[
import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.UIManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JComboBoxExample extends JFrame implements ActionListener { public JComboBox box; public JLabel label; public JButton button; JComboBoxExample() { super("JComboBox Example"); try { UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setSize(400,100); add(label); String languages []={"Kenya","Somalia","Rwanda", "Tanzania","South Africa","Namibia","Nigeria"}; box=new JComboBox(languages); box.setBounds(50,80,150,20); add(box); button=new JButton("Show"); button.setBounds(50,150,100,30); add(button); button.addActionListener(this); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JComboBoxExample(); } public void actionPerformed(ActionEvent event) { String lang="Country selected is "+ box.getItemAt(box.getSelectedIndex()); label.setText(lang); } }
]
Output:
Lessons you should learn from above program.
First you should note the difference between the two images.The look and Feel is different and this is courtesy of javax.swing.UIManager that i used to set WindowsClassicLookAndFeel using below code.
[try
{ UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { e.printStackTrace();
}]
Above class extends and implements ActionListener.[public class JComboBoxExample extends JFrame implements ActionListener]What does this mean?
This means that class JComboBoxExample has access to all JFrame methods.These are:-
[
setSize(400,400);
setLayout(null);setLocationRelativeTo(null);setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
]
setDefaultCloseOperation(EXIT_ON_CLOSE) is used to exit the application when the close button is clicked.
Implementing ActionListener interface means that you have to override public action performed method.
[
public void actionPerformed(ActionEvent event) { String lang="Country selected is "+ box.getItemAt(box.getSelectedIndex()); label.setText(lang); }
]
Code to add JComboBox to JFrame is [ box.setBounds(50,80,150,20);].Read more about setBounds component method here
You may also consider setting ToolTipText to JButton or JComboBox