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

public static void main(String args[]) {
    Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green,
            Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);/*from ww  w . j  a va  2s . c  o m*/
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    Color color = (Color) comboBox.getSelectedItem();
    ComboBoxEditor editor = new ColorComboBoxEditor(color);
    comboBox.setEditor(editor);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    contentPane.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color selectedColor = (Color) comboBox.getSelectedItem();
            label.setBackground(selectedColor);
        }
    };
    comboBox.addActionListener(actionListener);

    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 -> {//  w w  w .  jav  a2 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:TableDemoApplet.java

private static void createGUI(Container contentPane) {
    Object[][] rowData = new String[][] { { "98-43", "AraAra! SL" }, { "81-31", "Aragones Transports SA" },
            { "12-72", "Rocca SL" }, { "99-10", "Rodriguez e Hijos SA" }, { "00-65", "Rimbau Motors SL" } };
    JTable table = new JTable(rowData, new String[] { "Part No", "Provider" });

    JComboBox companyComboBox = new JComboBox(new Object[] { "AraAra! SL", "Aragones Transports SA", "Rocca SL",
            "Rodriguez e Hijos SA", "Rimbau Motors SL" });
    companyComboBox.setEditable(true);
    new S15WorkingBackspace(companyComboBox);

    // setup the ComboBoxCellEditor, DefaultCellEditor won't work!
    table.getColumnModel().getColumn(1).setCellEditor(new ComboBoxCellEditor(companyComboBox));

    table.setPreferredScrollableViewportSize(new Dimension(400, 100));
    JScrollPane scrollPane = new JScrollPane(table);

    contentPane.setLayout(new java.awt.FlowLayout());
    contentPane.add(scrollPane);//from   w ww.jav  a 2s.  co  m
    contentPane.add(new JTextField("HALLO!"));
}

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.IntHistogramVisualizer.java

/**
 * Creates the combo box (drop-down list) to choose between visualization of the histogram as bar chart or
 * as a scatter plot./*w  w w.  j  av a2 s  .com*/
 * 
 * @param aContainer Container control, to which the combo box is to be added.
 * @param aSelectedIndex Which choice is to be initially selected. This parameter must have one of the
 *        values <code>{1; 2}</code>.
 * @return The newly created combo box control.
 */
private static JComboBox addChoice(Box aContainer, int aSelectedIndex) {
    final String[] choices = new String[] { Messages.DI_SHOWHIST, Messages.DI_SHOWSCAT };
    JComboBox choiceCombo = new JComboBox(choices);
    choiceCombo.setSelectedIndex(aSelectedIndex);
    choiceCombo.setEditable(false);
    JPanel choicePanel = new JPanel();
    choicePanel.add(choiceCombo);
    aContainer.add(choicePanel);
    return choiceCombo;
}

From source file:com.intuit.tank.tools.debugger.ActionProducer.java

private static JComboBox getComboBox() {
    JComboBox cb = new JComboBox();
    cb.setEditable(true);
    Properties props = new Properties();
    File f = new File(DEBUGGER_PROPERTIES);
    InputStream in = null;/*from w  w  w . ja v  a 2 s.  c  om*/
    try {
        if (f.exists()) {
            in = new FileInputStream(f);
        }
        if (in == null) { // load default
            in = ActionProducer.class.getClassLoader().getResourceAsStream(DEBUGGER_PROPERTIES);
            OutputStream out = new FileOutputStream(f);
            try {
                IOUtils.copy(in, out);
            } catch (Exception e) {
                LOG.error("Cannot write properties: " + e, e);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
            in = new FileInputStream(f);
        }
        props.load(in);
        for (Object o : props.keySet()) {
            String key = (String) o;
            if (key.startsWith(TS_INSTANCE_START)) {
                String name = key.substring(TS_INSTANCE_START.length());
                String value = props.getProperty(key);
                cb.addItem(name + " (" + value + ")");
            }
        }
    } catch (Exception e) {
        LOG.error("Cannot read properties: " + e, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return cb;
}

From source file:JComboBoxDemo.java

public JComboBoxDemo() {
    JComboBox itemsComboBox = new JComboBox(new String[] { "A", "L", "M" });
    itemsComboBox.setEditable(true);
    itemsComboBox.setMaximumRowCount(3);
    this.getContentPane().add(itemsComboBox);
    itemsComboBox.setVisible(true);/*  w ww  .j  a  va2 s . com*/
    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
    this.validate();
    this.repaint();
}

From source file:Main.java

public Main() {
    JComboBox itemsComboBox = new JComboBox(new String[] { "A", "L", "M" });
    itemsComboBox.setEditable(true);
    itemsComboBox.setMaximumRowCount(3);
    this.getContentPane().add(itemsComboBox);
    itemsComboBox.setVisible(true);/*from   ww w .  j a  v a 2s. c o m*/
    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
    this.validate();
    this.repaint();
}

From source file:layout.CardLayoutDemo.java

public void addComponentToPane(Container pane) {
    //Put the JComboBox in a JPanel to get a nicer look.
    JPanel comboBoxPane = new JPanel(); //use FlowLayout
    String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
    JComboBox cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);//  w  w  w .j  av  a2s  . c  o  m

    //Create the "cards".
    JPanel card1 = new JPanel();
    card1.add(new JButton("Button 1"));
    card1.add(new JButton("Button 2"));
    card1.add(new JButton("Button 3"));

    JPanel card2 = new JPanel();
    card2.add(new JTextField("TextField", 20));

    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);

    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass() {
    // Build a mapping from book titles to their entries
    for (int i = 0; i < items.length; i++) {
        itemMap.put(items[i].getTitle(), items[i]);
    }// w  w w.  ja va 2 s  . co 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);
}

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. j a  va 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);
}