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

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D" };
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    comboBox.setUI((ComboBoxUI) MyComboBoxUI.createUI(comboBox));
    frame.add(comboBox, BorderLayout.NORTH);

    JComboBox comboBox2 = new JComboBox(labels);
    frame.add(comboBox2, BorderLayout.SOUTH);

    frame.setSize(300, 100);/* w w  w . j a v a 2  s.c  om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JToolBar toolbar = new JToolBar();
    ImageIcon icon = new ImageIcon("icon.gif");
    Action action = new AbstractAction("Button Label", icon) {
        public void actionPerformed(ActionEvent evt) {
        }//from  ww w  .j ava 2  s  . c o  m
    };

    JButton c1 = new JButton(action);
    c1.setText(null);
    c1.setMargin(new Insets(0, 0, 0, 0));
    toolbar.add(c1);

    JToggleButton c2 = new JToggleButton(action);
    c2.setText(null);
    c2.setMargin(new Insets(0, 0, 0, 0));
    toolbar.add(c2);

    JComboBox c3 = new JComboBox(new String[] { "A", "B", "C" });
    c3.setPrototypeDisplayValue("XXXXXXXX"); // Set a desired width
    c3.setMaximumSize(c3.getMinimumSize());
    toolbar.add(c3);
}

From source file:MainClass.java

public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };

    final JComboBox resizeModeComboBox = new JComboBox(modes);
    int defaultMode = 4;
    table.setAutoResizeMode(modeKey[defaultMode]);
    resizeModeComboBox.setSelectedIndex(defaultMode);
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int index = resizeModeComboBox.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }// ww  w.  j a  va 2 s .  c o  m
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(resizeModeComboBox, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);

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

From source file:Main.java

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);// www .j av a 2 s.c  o  m
    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);
}

From source file:TableSample2.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00", "un" }, { "two", "ni - \u4E8C", "deux" },
            { "three", "san - \u4E09", "trois" }, { "four", "shi - \u56DB", "quatre" },
            { "five", "go - \u4E94", "cinq" }, { "six", "roku - \u516D", "treiza" },
            { "seven", "shichi - \u4E03", "sept" }, { "eight", "hachi - \u516B", "huit" },
            { "nine", "kyu - \u4E5D", "neuf" }, { "ten", "ju - \u5341", "dix" } };
    Object options[] = { "un", "deux", "trois", "quatre", "cinq", "treiza", "sept", "huit", "neuf", "dix" };
    JComboBox comboBox = new JComboBox(options);
    comboBox.setMaximumRowCount(4);/* ww  w  .  ja  va 2s.c o m*/
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    Object headers[] = { "English", "Japanese", "French" };
    JFrame frame = new JFrame("JTable Anatomy");
    class CustomTableModel extends DefaultTableModel {
        public CustomTableModel(Object rowData[][], Object columnNames[]) {
            super(rowData, columnNames);
        }

        public Class getColumnClass(int col) {
            Vector v = (Vector) dataVector.elementAt(0);
            return v.elementAt(col).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            return true;
        }
    }
    JTable table = new JTable(new DefaultTableModel(rows, headers));

    //    ColumnModelUtilities.removeHeaders(table.getColumnModel());
    table.getColumnModel().getColumn(2).setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ResizeTable.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off",
            "Resize Subsequent Columns" };

    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
            JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };

    JComboBox resizeModeComboBox = new JComboBox(modes);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            JComboBox source = (JComboBox) e.getSource();
            int index = source.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }//from w w w  .j a v a  2 s . co  m
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(resizeModeComboBox, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:SelectingComboSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JComboBox comboBox = new JComboBox(labels);
    contentPane.add(comboBox, BorderLayout.SOUTH);

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);/*from   w  ww .  j a va 2  s. c o m*/
    JScrollPane sp = new JScrollPane(textArea);
    contentPane.add(sp, BorderLayout.CENTER);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            pw.print("Item: " + itemEvent.getItem());
            pw.print(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addItemListener(itemListener);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.print("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addActionListener(actionListener);

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

From source file:MyComboBoxModel.java

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

    JComboBox cbox = new JComboBox(new MyComboBoxModel());
    cbox.setMaximumRowCount(5);//from  w  w w. jav  a  2 s.co m
    frame.add(cbox);

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

From source file:EditableTable.java

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

    String[] columnTitles = { "First Name", "Last Name", "Weight (lb)", "Blood Group", "Age>20yrs" };
    Object[][] dataEntries = { { "Saravan", "Pantham", new Integer(50), "B", new Boolean(false) },
            { "Eric", "", new Integer(180), "O", new Boolean(true) },
            { "John", "", new Integer(120), "AB", new Boolean(false) },
            { "Mathew", "", new Integer(140), "A", new Boolean(true) }, };
    TableModel model = new EditableTableModel(columnTitles, dataEntries);
    JTable table = new JTable(model);
    table.createDefaultColumnsFromModel();

    String[] bloodGroups = { "A", "B", "AB", "O" };
    JComboBox comboBox = new JComboBox(bloodGroups);
    table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));

    frame.add(new JScrollPane(table));

    frame.setSize(300, 200);/*from   w  ww  .jav a 2 s  . c o  m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");

    String[] ops = { "+", "-", "*", "/" };

    JPanel gui = new JPanel(new BorderLayout(2, 2));
    JPanel labels = new JPanel(new GridLayout(0, 1));
    gui.add(labels, BorderLayout.WEST);
    labels.add(new JLabel("a"));
    labels.add(new JLabel("operand"));
    labels.add(new JLabel("b"));
    labels.add(new JLabel("="));

    JPanel controls = new JPanel(new GridLayout(0, 1));
    gui.add(controls, BorderLayout.CENTER);
    JTextField a = new JTextField(10);
    controls.add(a);//from   w  w w.j  a v  a 2s.c o m
    JComboBox operand = new JComboBox(ops);
    controls.add(operand);
    JTextField b = new JTextField(10);
    controls.add(b);
    JTextField output = new JTextField(10);
    controls.add(output);

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String expression = a.getText() + operand.getSelectedItem() + b.getText();
            try {
                Object result = engine.eval(expression);
                if (result == null) {
                    output.setText("Output was 'null'");
                } else {
                    output.setText(result.toString());
                }
            } catch (ScriptException se) {
                output.setText(se.getMessage());
            }
        }
    };

    operand.addActionListener(al);
    a.addActionListener(al);
    b.addActionListener(al);

    JOptionPane.showMessageDialog(null, gui);
}