Get Item in a JComboBox one by one in Java

Description

The following code shows how to get Item in a JComboBox one by one.

Example


import java.awt.BorderLayout;
//from   w w w  .ja v  a 2  s. co  m
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main {

  public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    // Get number of items
    int num = comboBox.getItemCount();

    // Get items
    for (int i = 0; i < num; i++) {
      Object item = comboBox.getItemAt(i);
      System.out.println(item);
    }

    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Get Item in a JComboBox one by one in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer