Example usage for javax.swing JComboBox JComboBox

List of usage examples for javax.swing JComboBox JComboBox

Introduction

In this page you can find the example usage for javax.swing JComboBox JComboBox.

Prototype

public JComboBox(Vector<E> items) 

Source Link

Document

Creates a JComboBox that contains the elements in the specified Vector.

Usage

From source file:Main.java

public Main() {

    super();//  w  w w  .  j ava 2  s  .  co  m
    java.util.List<String> list = new ArrayList<String>();

    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    Object[] arrayObject = list.toArray();
    String[] data = Arrays.copyOf(arrayObject, arrayObject.length, String[].class); // java 1.6+
    JComboBox<String> combo = new JComboBox<>(data);

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(combo, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    DefaultTableModel m = new DefaultTableModel(new Object[][] { { "2", 2, 3 }, { "1", 4, 5 } },
            new Object[] { 1, 2, 3 });
    JTable t = new JTable(m);
    t.getColumnModel().getColumn(0)//from   w  w w. j  av a2s  .c  o m
            .setCellEditor(new DefaultCellEditor(new JComboBox(new String[] { "1", "2" })));
    t.getColumnModel().getColumn(0).setCellRenderer(getCellRenderer());
    t.setRowHeight(25);
    getContentPane().add(new JScrollPane(t));
    pack();
}

From source file:Main.java

Main(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel jp = new JPanel();

    Vector v = new Vector();
    v.add("A");//from w ww.j  a v  a  2s  . c  o m
    v.add("B");
    v.add("C");

    jcb = new JComboBox(v);
    jcb.setEditable(true);

    jcb.getActionMap().put("selectNext", new DownAction());

    jp.setPreferredSize(new Dimension(200, 35));
    jp.add(jcb);
    getContentPane().add(jp);

    pack();
    setVisible(true);
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel jp = new JPanel();

    Vector v = new Vector();
    v.add("A");/*w ww . j a  va2s. c  om*/
    v.add("B");
    v.add("C");

    jcb = new JComboBox(v);
    jcb.setEditable(true);

    jcb.getActionMap().put("selectNext", new DownAction());

    jp.setPreferredSize(new Dimension(200, 35));
    jp.add(jcb);
    getContentPane().add(jp);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    Vector model = new Vector();
    model.addElement(new Item(1, "A"));
    model.addElement(new Item(2, "B"));
    model.addElement(new Item(4, "C"));
    model.addElement(new Item(3, "D"));

    JComboBox comboBox = new JComboBox(model);
    comboBox.setRenderer(new ItemRenderer());
    comboBox.addActionListener(this);
    getContentPane().add(comboBox, BorderLayout.SOUTH);
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());

    String[] items1 = { "Red", "Blue", "Green" };
    JComboBox<String> comboBox1 = new JComboBox<String>(items1);
    DefaultCellEditor dce1 = new DefaultCellEditor(comboBox1);
    editors.add(dce1);//from  ww  w.  j a va 2s .c  o m

    String[] items2 = { "Circle", "Square", "Triangle" };
    JComboBox<String> comboBox2 = new JComboBox<String>(items2);
    DefaultCellEditor dce2 = new DefaultCellEditor(comboBox2);
    editors.add(dce2);

    String[] items3 = { "Apple", "Orange", "Banana" };
    JComboBox<String> comboBox3 = new JComboBox<String>(items3);
    DefaultCellEditor dce3 = new DefaultCellEditor(comboBox3);
    editors.add(dce3);

    Object[][] data = { { "Color", "Red" }, { "Shape", "Square" }, { "Fruit", "Banana" }, { "Plain", "Text" } };
    String[] columnNames = { "Type", "Value" };
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        public TableCellEditor getCellEditor(int row, int column) {
            int modelColumn = convertColumnIndexToModel(column);

            if (modelColumn == 1 && row < 3)
                return editors.get(row);
            else
                return super.getCellEditor(row, column);
        }
    };

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
}

From source file:Main.java

public Main() {
    Vector model = new Vector();
    model.addElement(new Item(new ImageIcon("copy16.gif"), "copy"));
    model.addElement(new Item(new ImageIcon("add16.gif"), "add"));
    model.addElement(new Item(new ImageIcon("about16.gif"), "about"));

    JComboBox comboBox;//w  w w.j a v  a 2 s  . c  om

    comboBox = new JComboBox(model);
    comboBox.setRenderer(new ItemRenderer());
    getContentPane().add(comboBox, BorderLayout.SOUTH);
}

From source file:Main.java

public Main() {
    Vector model = new Vector();
    model.addElement(new Item(1, "A"));
    model.addElement(new Item(2, "B"));
    model.addElement(new Item(3, "C"));
    model.addElement(new Item(4, "D"));

    JComboBox comboBox = new JComboBox(model);
    comboBox.addActionListener(this);
    comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
    getContentPane().add(comboBox, BorderLayout.NORTH);

    comboBox = new JComboBox(model);
    comboBox.setRenderer(new ItemRenderer());
    comboBox.addActionListener(this);
    getContentPane().add(comboBox, BorderLayout.SOUTH);
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel jp = new JPanel();

    Vector v = new Vector();
    v.add("A");//  w  ww  .  j  a v  a 2s  .  c  om
    v.add("B");
    v.add("C");

    jcb = new JComboBox(v);
    jcb.setEditable(true);

    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, Event.CTRL_MASK);

    jcb.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ks, "clearEditor");
    jcb.getActionMap().put("clearEditor", new ClearEditorAction());

    jp.setPreferredSize(new Dimension(200, 35));
    jp.add(jcb);
    getContentPane().add(jp);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    setLayout(new GridLayout(0, 1));

    DefaultComboBoxModel<ComboColor> cModel = new DefaultComboBoxModel<>(ComboColor.values());
    JComboBox<ComboColor> combo = new JComboBox<>(cModel);
    add(createComboLabelPanel(1, combo));
    comboList.add(combo);//  w w w.  ja  v a 2  s.  c  o  m

}