Example usage for javax.swing JComboBox addItemListener

List of usage examples for javax.swing JComboBox addItemListener

Introduction

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

Prototype

public void addItemListener(ItemListener aListener) 

Source Link

Document

Adds an ItemListener.

Usage

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new GridLayout(0, 1, 5, 5));

    String[] speciesName = { "1", "2", "3" };
    String[][] breedName = { { "A", "P", "A" }, { "B", "P", "S" }, { "DDo", "A", "P" } };
    JComboBox<String> petSpecies = new JComboBox<>(speciesName);
    JComboBox<String> petBreed = new JComboBox<>();
    petSpecies.addItemListener(e -> {
        int ii = petSpecies.getSelectedIndex();
        ComboBoxModel cbm = new DefaultComboBoxModel(breedName[ii]);
        petBreed.setModel(cbm);/*from  ww w.j a va 2 s . c  o m*/
        petBreed.requestFocusInWindow();
    });
    gui.add(petSpecies);
    gui.add(petBreed);

    JOptionPane.showMessageDialog(null, gui);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "item1", "item2" };
    JComboBox cb = new JComboBox(items);
    cb.setEditable(true);// w ww  .j  a v  a  2  s.c  o m

    MyItemListener actionListener = new MyItemListener();
    cb.addItemListener(actionListener);
}

From source file:Main.java

public static void main(String[] args) {
    Object[] items = new Object[] { "Dog", "Cat", "Other" };
    DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);
    JComboBox comboBox = new JComboBox(dcbm);
    comboBox.setPreferredSize(new Dimension(200, 20));
    comboBox.addItemListener(e -> {
        Object selectedItem = comboBox.getSelectedItem();
        boolean editable = selectedItem instanceof String && ((String) selectedItem).equals("Other");
        comboBox.setEditable(editable);/*from   w  w w .j  a  v a 2  s  .c  om*/
    });
    comboBox.getEditor().addActionListener(e -> {
        Object newItem = comboBox.getEditor().getItem();
        DefaultComboBoxModel d = (DefaultComboBoxModel) comboBox.getModel();
        d.addElement(newItem);
        d.setSelectedItem(newItem);

    });

    JPanel content = new JPanel(new FlowLayout());
    content.add(new JLabel("Test:"));
    content.add(comboBox);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(content);
    frame.pack();
    frame.setVisible(true);
}

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());
            }/* w  w  w  .  ja  v a  2  s  .co  m*/
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.setVisible(true);
}

From source file:ItemTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Source: " + name(e.getSource()));
            System.out.println("Item: " + name(e.getItem()));
            int state = e.getStateChange();
            System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"));
        }//from  ww w.  j a v a 2  s.co m

        private String name(Object o) {
            if (o instanceof JComponent) {
                JComponent comp = (JComponent) o;
                return comp.getName();
            } else {
                return o.toString();
            }
        }
    };

    JPanel panel = new JPanel(new GridLayout(0, 1));
    ButtonGroup group = new ButtonGroup();
    JRadioButton option = new JRadioButton("French Fries", true);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Onion Rings", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Ice Cream", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    contentPane.add(panel, BorderLayout.NORTH);

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setName("Combo");
    jc.addItemListener(listener);
    jc.setMaximumRowCount(4);
    contentPane.add(jc, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    String items[] = { "Item1", "item1" };
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JComboBox<String> combo = new JComboBox<>(items);
    combo.setEditable(true);//w ww  .j  a  v a 2 s. com

    JTextField txt = new JTextField(10);
    panel.add(combo);
    panel.add(txt);
    f.add(panel);
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ie) {
            String str = (String) combo.getSelectedItem();
            txt.setText(str);
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 100);
    f.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]);
        }/* w w w  . ja  v  a2s .  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[]) {
    String[] mainData = { "-Select-", "Sel 1", "Sel 2", "Sel 3" };
    String[] subData1 = { "Sub Sel 11", "Sub Sel 12", "Sub Sel 13" };
    String[] subData2 = { "Sub Sel 21", "Sub Sel 22", "Sub Sel 23" };
    String[] subData3 = { "Sub Sel 31", "Sub Sel 32", "Sub Sel 33" };
    DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
    JComboBox box1 = new JComboBox(mainData), box2 = new JComboBox();
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.add(box1);/*  w w w. jav a 2 s .  c o m*/
    frame.add(box2);
    box2.setVisible(false);
    box2.setModel(boxModel);
    frame.setBounds(200, 200, 500, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    box1.addItemListener(e -> {
        box2.setVisible(true);
        boxModel.removeAllElements();
        if (box1.getSelectedIndex() == 0) {
            box2.setVisible(false);
        } else if (box1.getSelectedIndex() == 1) {
            for (String s : subData1) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 2) {
            for (String s : subData2) {
                boxModel.addElement(s);
            }
        } else if (box1.getSelectedIndex() == 3) {
            for (String s : subData3) {
                boxModel.addElement(s);
            }
        }
    });
}

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]);
        }/*from w w w  .  j ava 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:ResizeTable.java

public static void main(String args[]) {

    Object rowData[][] = { { "1", "one", "ichi - \u4E00", "un", "I" },
            { "2", "two", "ni - \u4E8C", "deux", "II" }, { "3", "three", "san - \u4E09", "trois", "III" },
            { "4", "four", "shi - \u56DB", "quatre", "IV" }, { "5", "five", "go - \u4E94", "cinq", "V" },
            { "6", "six", "roku - \u516D", "treiza", "VI" }, { "7", "seven", "shichi - \u4E03", "sept", "VII" },
            { "8", "eight", "hachi - \u516B", "huit", "VIII" }, { "9", "nine", "kyu - \u4E5D", "neur", "IX" },
            { "10", "ten", "ju - \u5341", "dix", "X" } };

    String columnNames[] = { "#", "English", "Japanese", "French", "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);
    int defaultMode = 4;
    table.setAutoResizeMode(modeKey[defaultMode]);
    resizeModeComboBox.setSelectedIndex(defaultMode);
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            JComboBox source = (JComboBox) e.getSource();
            int index = source.getSelectedIndex();
            table.setAutoResizeMode(modeKey[index]);
        }/*from  ww  w. j a  v a2s  . co  m*/
    };
    resizeModeComboBox.addItemListener(itemListener);

    JFrame frame = new JFrame("Resizing Table");
    Container contentPane = frame.getContentPane();

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

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