Example usage for javax.swing DefaultComboBoxModel DefaultComboBoxModel

List of usage examples for javax.swing DefaultComboBoxModel DefaultComboBoxModel

Introduction

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

Prototype

public DefaultComboBoxModel(Vector<E> v) 

Source Link

Document

Constructs a DefaultComboBoxModel object initialized with a vector.

Usage

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(model);
    comboBox1.setMaximumRowCount(5);//  w w w. j  ava2 s  .c  o m
    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: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);/*w  w w . ja  va  2s . 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: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 -> {/*from  w ww.j a v  a 2  s.c o m*/
        Object selectedItem = comboBox.getSelectedItem();
        boolean editable = selectedItem instanceof String && ((String) selectedItem).equals("Other");
        comboBox.setEditable(editable);
    });
    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) {
    JFrame frame = new JFrame();

    String[] list = { "Hello 1", "Hello 2", "Hello 3", "Hello 4" };
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(list);
    JComboBox<String> comboBox = new JComboBox<>(model);
    frame.add(comboBox, BorderLayout.NORTH);

    final JTextField textField = new JTextField(30);
    frame.add(textField, BorderLayout.SOUTH);
    frame.add(new JLabel("Type something, then press enter", JLabel.CENTER));

    textField.addActionListener(ae -> {
        String text = textField.getText();
        model.addElement(text);//from   w w w. j  a  va  2s .  c  om
        comboBox.setSelectedItem(text);
        textField.setText("");

    });

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

From source file:SharedDataBetweenComboBoxSample.java

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

    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(model);
    comboBox1.setEditable(true);/*  w  w w  .j  a  v a2  s  .c  o  m*/

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

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("New Added");
        }
    };
    button.addActionListener(actionListener);

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

From source file:Main.java

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

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox<String> comboBox1 = new JComboBox<String>(model);
    comboBox1.setMaximumRowCount(5);//from  w  ww.  jav  a  2 s  .  com
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);

    JList<String> jlist = new JList<String>(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("a");
        }
    };
    button.addActionListener(actionListener);

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

From source file:MainClass.java

public static void main(final String args[]) {
    final 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);

    JComboBox comboBox1 = new JComboBox(model);
    comboBox1.setMaximumRowCount(5);//  ww  w  .  j  a v  a 2  s  .c o  m
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);

    JList jlist = new JList(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("a");
        }
    };
    button.addActionListener(actionListener);

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

From source file:SharedDataBetweenComboBoxAndListSample.java

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

    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(model);
    comboBox1.setEditable(true);/*from w  w w .jav  a 2 s.  co m*/

    panel.add(comboBox1);
    frame.add(panel, BorderLayout.NORTH);

    JList jlist = new JList(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("New Added");
        }
    };
    button.addActionListener(actionListener);

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

From source file:Main.java

public static void main(String[] args) {
    int TIMER_DELAY = 2000;
    String[] data = { "A", "B", "C", "D" };

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(data);

    JComboBox<String> combobox = new JComboBox<>(model);
    JList<String> jlist = new JList<>(model);

    new Timer(TIMER_DELAY, new ActionListener() {
        private int count = 0;

        public void actionPerformed(ActionEvent e) {
            model.addElement("count: " + count);
            count++;// ww w  .  j a  va  2s . c o m
        }
    }).start();

    JPanel comboPanel = new JPanel();
    comboPanel.add(combobox);

    JPanel listPanel = new JPanel();
    listPanel.add(new JScrollPane(jlist));

    JPanel panel = new JPanel(new GridLayout(1, 0));
    panel.add(comboPanel);
    panel.add(listPanel);
    panel.setPreferredSize(new Dimension(400, 200));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(panel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    Vector<String> vec = new Vector<String>();
    vec.add("a");
    vec.add("b");
    vec.add("c");

    final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(vec);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox<String> comboBox1 = new JComboBox<String>(model);
    comboBox1.setMaximumRowCount(5);//w ww.j  a  va 2  s . c  o m
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);

    JList<String> jlist = new JList<String>(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("a");
        }
    };
    button.addActionListener(actionListener);

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