Example usage for javax.swing JComboBox setEditable

List of usage examples for javax.swing JComboBox setEditable

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "If true, the user can type a new value in the combo box.")
public void setEditable(boolean aFlag) 

Source Link

Document

Determines whether the JComboBox field is editable.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox combo = new JComboBox();
    combo.setEditable(true);
    for (int i = 0; i < 10; i++) {
        combo.addItem(i);//from   w  w w  .  ja v  a2  s.co  m
    }
    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: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);

    JComboBox comboBox2 = new JComboBox(model);
    comboBox2.setEditable(true);/*from   w  w w . j  ava  2s .  c om*/
    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: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);

    panel.add(comboBox1);/*from   w  w  w.  j  a v  a  2 s .c o  m*/
    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[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JComboBox<String> comboBox = new JComboBox<>(labels);
    comboBox.setEditable(true);

    comboBox.addActionListener(new ActionListener() {
        boolean found = false;

        @Override/*from w  ww  .  ja  v a2s . c  o m*/
        public void actionPerformed(ActionEvent actionEvent) {
            String s = (String) comboBox.getSelectedItem();
            for (int i = 0; i < comboBox.getItemCount(); i++) {
                if (comboBox.getItemAt(i).toString().equals(s)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("Added: " + s);
                comboBox.addItem(s);
            }
            found = false;
        }
    });

    frame.add(comboBox);

    frame.pack();
    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(model);
    comboBox1.setMaximumRowCount(5);/*ww  w .j  av  a  2s  .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(String[] args) {
    JComboBox<String> comboBox = new JComboBox<>(new String[] { "A", "B", "C" });
    comboBox.setEditable(true);

    JTextField editorComponent = (JTextField) comboBox.getEditor().getEditorComponent();
    editorComponent.addActionListener(e -> {
        editorComponent.transferFocus();
    });/*w w w  . j a  va  2 s . co m*/

    JPanel panel = new JPanel(new FlowLayout());
    panel.add(new JLabel("Field 1"));
    panel.add(comboBox);
    panel.add(new JLabel("Field 2"));
    panel.add(new JTextField(10));
    panel.add(new JLabel("Field 3"));
    panel.add(new JTextField(10));

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

    Container c = frame.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(panel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

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

    final JComboBox comboBox = new JComboBox(labels);
    comboBox.setMaximumRowCount(5);/*from   w w w. j ava 2s  .c  o  m*/
    comboBox.setEditable(true);
    frame.add(comboBox, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + comboBox.getSelectedItem());
            System.out.println(", Position: " + comboBox.getSelectedIndex());
        }
    };
    comboBox.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);//w  w w . j a  v a2 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:ColorComboBoxEditor.java

public static void main(String args[]) {
    Color colors[] = { Color.RED, Color.BLUE, Color.BLACK, Color.WHITE };
    JFrame frame = new JFrame("Editable JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setEditable(true);
    comboBox.setEditor(new ColorComboBoxEditor(Color.RED));
    frame.add(comboBox, BorderLayout.NORTH);

    frame.setSize(300, 200);// ww w . j  a v  a2 s .  co  m
    frame.setVisible(true);
}

From source file:EditComboBox.java

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

    final JComboBox comboBox = new JComboBox(labels);
    comboBox.setMaximumRowCount(5);/*from  w ww.  jav  a2  s .co m*/
    comboBox.setEditable(true);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            textArea.append("Selected: " + comboBox.getSelectedItem());
            textArea.append(", Position: " + comboBox.getSelectedIndex());
            textArea.append(System.getProperty("line.separator"));
        }
    };
    comboBox.addActionListener(actionListener);

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