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

Main() {
    String[] items = { "Item1", "Item2" };
    comboBox = new JComboBox<>(items);
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(comboBox);//from w  ww . j  av a  2  s .  co  m
    comboBox.setUI(new MyUI());
}

From source file:Main.java

public Main() {
    String[] comboTypes = { "Numbers", "Alphabets", "Symbols" };
    JComboBox<String> comboTypesList = new JComboBox<>(comboTypes);
    comboTypesList.setSelectedIndex(2);/*www  .  j ava 2  s . co  m*/
    comboTypesList.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JComboBox jcmbType = (JComboBox) e.getSource();
            String cmbType = (String) jcmbType.getSelectedItem();
            System.out.println(cmbType);
        }
    });
    setLayout(new BorderLayout());
    add(comboTypesList, BorderLayout.NORTH);
}

From source file:Main.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    String[] sList = new String[] { "Spring", "Summer", "Fall", "Winter" };
    JComboBox<String> seasons = new JComboBox<>(sList);

    seasons.addItemListener((ItemEvent e) -> {
        Object item = e.getItem();
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Item has been selected
            System.out.println(item + "  has  been  selected");
        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
            // Item has been deselected
            System.out.println(item + "  has  been  deselected");
        }//from   w  w w .j  ava 2s .c o  m
    });

    Container contentPane = this.getContentPane();
    contentPane.add(seasons, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    String[] items = { "Item1", "Item2", "Item3", "Item4", "Item5" };
    JComboBox<String> comboBox = new JComboBox<>(items);
    add(comboBox);//from w  ww . j a va  2 s.c  o  m

    comboBox.addPopupMenuListener(new PopupMenuListener() {
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            JComboBox<String> comboBox = (JComboBox<String>) e.getSource();
            BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0);
            JList list = popup.getList();
            list.setSelectedIndex(2);
        }

        public void popupMenuCanceled(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

    });
}

From source file:Main.java

Main() {
    comboBox = new JComboBox<>(new String[] { "111", "222", "333" });
    JList<String> list = new JList<>(model = new DefaultListModel<>());
    add(comboBox, BorderLayout.SOUTH);
    add(new JScrollPane(list));

    addKeyBindings((JComponent) getContentPane());
    addKeyBindings(comboBox);// w w  w .j  a  v a2s  .c o m

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    Object[] items = { Color.red, Color.green, Color.blue };
    JComboBox comboBox = new JComboBox(items);
    comboBox.setRenderer(new ColorRenderer(comboBox));
    getContentPane().add(comboBox, BorderLayout.NORTH);
    add(new JTextField(), BorderLayout.SOUTH);
}

From source file:Main.java

Main() {
    JFrame f1 = new JFrame("Selection");
    Container f = new Container();
    f.setLayout(new FlowLayout());

    String s[] = { "Red", "Green", "Yellow", "Black" };
    c = new JComboBox(s);
    JPanel p = new JPanel();

    c.addItemListener(this);

    f1.add(p);// ww  w  . j a v a  2s.c  o m
    p.add(c);
    f1.setSize(500, 500);
    f1.setVisible(true);
}

From source file:Main.java

public Main() {
    comboBox = new JComboBox(new String[] { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" });
    add(comboBox, BorderLayout.PAGE_START);
    JFrame frame = new JFrame("Main");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comboBox);/*from  w  ww. j  ava 2 s . c o m*/
    frame.pack();
    frame.setVisible(true);

    comboBox.showPopup();
    Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
    BasicComboPopup popup = (BasicComboPopup) child;
    popup.setName("BasicComboPopup");
    JList list = popup.getList();
    Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
    JScrollPane scrollPane = (JScrollPane) c;

    Window mainFrame = SwingUtilities.windowForComponent(comboBox);

    System.out.println(mainFrame.getName());
    Window popupWindow = SwingUtilities.windowForComponent(popup);
    System.out.println(popupWindow);
    Window popupWindowa = SwingUtilities.windowForComponent(c);
    System.out.println(popupWindowa);

    Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox);

    System.out.println(mainFrame1);
    Window popupWindow1 = SwingUtilities.getWindowAncestor(popup);
    System.out.println(popupWindow1);

    Component mainFrame2 = SwingUtilities.getRoot(comboBox);

    System.out.println(mainFrame2.getName());
    Component popupWindow2 = SwingUtilities.getRoot(popup);
    System.out.println(popupWindow2);

    if (popupWindow != mainFrame) {
        popupWindow.pack();
    }
}

From source file:Main.java

public Main() {
    String[] items = { "Select Item", "Color", "Shape", "Fruit" };
    mainComboBox = new JComboBox(items);
    mainComboBox.addActionListener(this);

    getContentPane().add(mainComboBox, BorderLayout.WEST);

    subComboBox = new JComboBox();
    getContentPane().add(subComboBox, BorderLayout.EAST);

    String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
    subItems.put(items[1], subItems1);//from  www .  j a va  2  s . c  om

    String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
    subItems.put(items[2], subItems2);

    String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
    subItems.put(items[3], subItems3);
}

From source file:Main.java

public static JComboBox generateComboBoxFor(Object... objs) {
    Container c = search(objs, Container.class);
    ComboBoxModel model = null;//from   w  ww .j a  va  2  s  .  c  o  m
    Vector items = null;
    Object[] itms = null;
    Border border = search(objs, Border.class);
    Color color = search(objs, Color.class);
    ComboBoxUI ui = search(objs, ComboBoxUI.class);
    JComboBox combo = (model = search(objs, ComboBoxModel.class)) == null
            ? (items = search(objs, Vector.class)) == null ? new JComboBox(itms = search(objs, Object[].class))
                    : new JComboBox(items)
            : new JComboBox(model);
    if (border != null)
        combo.setBorder(border);
    if (ui != null)
        combo.setUI(ui);
    if (color != null)
        combo.setBackground(color);
    addJContainerListeners(combo, objs);
    addToContainer(c, combo);
    return combo;
}