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() 

Source Link

Document

Creates a JComboBox with a default data model.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> emptyBox = new JComboBox<String>();
    emptyBox.addActionListener(new ActionListener() {
        @Override/*from  w  w  w  . ja va 2 s .  co m*/
        public void actionPerformed(ActionEvent e) {
            Thread.dumpStack();
        }
    });
    emptyBox.addItem("test");
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> box = new JComboBox<>();
    box.addItem("One");
    box.addItem("Two");
    box.addItem("Three");

    box.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println(e.getItem());
            }/*from   w w  w.j a  va 2s . c  om*/
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.setVisible(true);
}

From source file:AddingItemToComboBox.java

License:asdf

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox jComboBox1 = new JComboBox();
    jComboBox1.addItem("asdf");
    Object cmboitem = jComboBox1.getSelectedItem();
    System.out.println(cmboitem);

    frame.add(jComboBox1);//from  ww  w .  j a  va2  s .  com

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

From source file:Main.java

public static void main(String[] args) {
    JComboBox c = new JComboBox();
    c.addPopupMenuListener(new PopupMenuListener() {

        @Override/*w  w w .java 2 s . c o m*/
        public void popupMenuCanceled(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            System.out.println(e.getSource());
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new FlowLayout());
    f.getContentPane().add(c);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox combo = new JComboBox();
    combo.setEditable(true);//from   www  .j  ava 2 s  . c o m
    for (int i = 0; i < 10; i++) {
        combo.addItem(i);
    }
    JLabel tip = new JLabel();
    tip.setText("Outside combobox");
    JPanel panel = new JPanel();
    panel.add(combo);
    panel.add(tip);
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            tip.setText("Outside combobox");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
            tip.setText(c != null && SwingUtilities.isDescendingFrom(c, combo) ? "Inside combo box"
                    : "Outside combobox");
        }
    });
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setSize(450, 250);/*from w  ww  . ja  va 2s  . c om*/

    JTable table = new JTable(5, 5);

    TableColumn testColumn = table.getColumnModel().getColumn(0);

    JComboBox<String> comboBox = new JComboBox<>();
    comboBox.addItem("This");
    comboBox.addItem("is");
    comboBox.addItem("a");
    comboBox.addItem("Sample program");
    testColumn.setCellEditor(new DefaultCellEditor(comboBox));

    frame.add(table);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JPanel p = new JPanel();

    p.setLayout(new GridLayout(2, 1));
    JList<String> lista = new JList<>(new String[] { "1", "2", "3", "4" });
    p.add(new JScrollPane(lista));
    JComboBox<String> combo = new JComboBox<>();
    for (int i = 0; i < 100; i++) {
        combo.addItem(Integer.toString(i));
        p.add(combo);/* w  ww  .  ja v  a 2 s  .  co m*/
    }

    JFrame f = new JFrame();
    f.getContentPane().add(p, BorderLayout.CENTER);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 200);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox<Object> combo = new JComboBox<>();
    URL url = new Main().getClass().getResource("animated.gif");
    combo.setModel(new DefaultComboBoxModel(new Object[] { makeImageIcon(url, combo, 0) }));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(combo);//from   w w w . ja v  a2  s  .c  o m
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);

    JFrame frame = new JFrame("Shared Data");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    JComboBox comboBox1 = new JComboBox();
    comboBox1.setModel(model);/*from  w ww. j  a  v  a2  s .  c o  m*/
    comboBox1.setMaximumRowCount(5);
    comboBox1.setEditable(true);

    JComboBox comboBox2 = new JComboBox(model);
    comboBox2.setMaximumRowCount(5);
    comboBox2.setEditable(true);
    panel.add(comboBox1);
    panel.add(comboBox2);
    frame.add(panel, BorderLayout.NORTH);

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

From source file:DateComboBoxRenderer.java

public static void main(String[] str) {
    JComboBox combo = new JComboBox();
    GregorianCalendar calendar = new GregorianCalendar();
    combo.addItem(calendar.getTime());//  www .j ava 2  s  . c om

    calendar.roll(GregorianCalendar.DAY_OF_MONTH, 1);
    combo.addItem(calendar.getTime());

    combo.setRenderer(new DateComboBoxRenderer());

    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.add(new JLabel("Date Combo: "));
    panel.add(combo);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}