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:AllAvailableFontsComboBox.java

public AllAvailableFontsComboBox() {
    add(new JLabel("Fonts"));

    GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String envfonts[] = gEnv.getAvailableFontFamilyNames();
    Vector vector = new Vector();
    for (int i = 1; i < envfonts.length; i++) {
        vector.addElement(envfonts[i]);//from w w w. j  a v a 2  s  . c om
    }
    fonts = new JComboBox(vector);
    add(fonts);
}

From source file:Main.java

public Main() {
    label = new JLabel("Select a Customer");
    add(label, BorderLayout.NORTH);

    Customer customers[] = new Customer[6];
    customers[0] = new Customer("A", 1);
    customers[1] = new Customer("B", 6);
    customers[2] = new Customer("C", 2);
    customers[3] = new Customer("D", 3);
    customers[4] = new Customer("E", 4);
    customers[5] = new Customer("F", 5);

    combo = new JComboBox(customers);
    combo.addItemListener(e -> {//  w  w w  . j a v  a2  s  .c  om
        Customer c = (Customer) e.getItem();
        label.setText("You selected customer id: " + c.getId());
    });
    JPanel panel = new JPanel();
    panel.add(combo);
    add(panel, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 200);
    setVisible(true);
}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(320, 240));
    add(odometer);//  w ww  . j  a va2s . c o  m
    JComboBox colorBox = new JComboBox(new String[] { "a", "b" });
    colorBox.addActionListener(e -> {
        tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
    });
    this.add(colorBox);
    timer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            km += random.nextInt(100);
            odometer.setText(df.format(km));
        }
    });
    timer.start();
}

From source file:Main.java

public Main() {
    options.add(createOptionPane("Plain Message", JOptionPane.PLAIN_MESSAGE));
    options.add(createOptionPane("Error Message", JOptionPane.ERROR_MESSAGE));
    options.add(createOptionPane("Information Message", JOptionPane.INFORMATION_MESSAGE));
    options.add(createOptionPane("Warning Message", JOptionPane.WARNING_MESSAGE));
    options.add(createOptionPane("Want to do something?", JOptionPane.QUESTION_MESSAGE));
    items = new Object[] { "First", "Second", "Third" };
    JComboBox choiceCombo = new JComboBox(items);
    options.add(new JOptionPane(choiceCombo, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION),
            "Question Message");
    JFrame frame = new JFrame("JOptionPane'Options");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(options, BorderLayout.CENTER);
    frame.pack();//from  w ww. jav a  2s .  c  o m
    frame.setVisible(true);
}

From source file:CombiningShapes.java

public CombiningShapes() {
    mShapeOne = new Ellipse2D.Double(40, 20, 80, 80);
    mShapeTwo = new Rectangle2D.Double(60, 40, 80, 80);
    setBackground(Color.white);//w  w  w.j a  va  2  s .  co  m
    setLayout(new BorderLayout());

    JPanel controls = new JPanel();

    mOptions = new JComboBox(new String[] { "outline", "add", "intersection", "subtract", "exclusive or" });

    mOptions.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            repaint();
        }
    });
    controls.add(mOptions);
    add(controls, BorderLayout.SOUTH);
}

From source file:ComboBoxWithItemChangedListener.java

public ComboPanel() {
    final JComboBox combo = new JComboBox(treasure);
    combo.setMaximumRowCount(5);//ww w .  j  a  v a  2s . c  o  m
    combo.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Combo: " + combo.getSelectedItem());
        }
    });
    combo.setSelectedIndex(4);
    add(combo);
}

From source file:Main.java

private TableCellRenderer getCellRenderer() {
    return new TableCellRenderer() {
        JComboBox<String> box = new JComboBox<>(new String[] { "1", "2" });

        @Override//  w w w. j  a  v a  2 s  .c o m
        public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3,
                int arg4, int arg5) {
            box.setSelectedItem(arg1);
            return box;
        }
    };
}

From source file:Main.java

public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);/*w ww .ja va 2  s .c  om*/

    String comboPrefix = "Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    JList list = new JList(vector);
    list.setSelectedIndex(1);
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
}

From source file:Main.java

public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);//from  w ww . j  a  v a2 s. c  om

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1);
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:Main.java

public Main() {
    // Build a mapping from book titles to their entries
    for (int i = 0; i < items.length; i++) {
        itemMap.put(items[i].getTitle(), items[i]);
    }//from   w  w  w .ja v  a 2  s .  c o m

    setLayout(new BorderLayout());

    JComboBox bookCombo = new JComboBox(items);
    bookCombo.setEditable(true);
    bookCombo.setEditor(new MyComboBoxEditor(itemMap, items[0]));
    bookCombo.setMaximumRowCount(4);
    bookCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("You chose " + ((JComboBox) e.getSource()).getSelectedItem() + "!");
        }
    });
    bookCombo.setActionCommand("Hello");
    add(bookCombo, BorderLayout.CENTER);
}