Java Swing How to - Fill integer, double, boolean and Icon data to JComboBox








Question

We would like to know how to fill integer, double, boolean and Icon data to JComboBox.

Answer

import java.awt.GridLayout;
import java.util.Vector;
//from   w w w  .  j  a  va2 s .com
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class Main {
  public static void main(String[] args) {
    Vector<Double> doubleVector = new Vector<Double>();
    Vector<Integer> integerVector = new Vector<Integer>();
    Vector<Boolean> booleanVector = new Vector<Boolean>();
    Vector<Icon> iconVector = new Vector<Icon>();
    Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));

    doubleVector.addElement(1.001);
    doubleVector.addElement(10.00);
    doubleVector.addElement(0.95);
    doubleVector.addElement(4.2);
    JComboBox comboBoxDouble = new JComboBox(doubleVector);
    integerVector.addElement(1);
    integerVector.addElement(2);
    integerVector.addElement(3);
    integerVector.addElement(4);
    JComboBox comboBoxInteger = new JComboBox(integerVector);
    booleanVector.add(Boolean.TRUE);
    booleanVector.add(Boolean.FALSE);
    JComboBox comboBoxBoolean = new JComboBox(booleanVector);
    iconVector.addElement(icon1);
    iconVector.addElement(icon2);
    iconVector.addElement(icon3);
    iconVector.addElement(icon4);
    JComboBox comboBoxIcon = new JComboBox(iconVector);
    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(2, 2, 5, 5));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comboBoxDouble);
    frame.add(comboBoxInteger);
    frame.add(comboBoxBoolean);
    frame.add(comboBoxIcon);
    frame.pack();
    frame.setVisible(true);
  }
}