Example usage for javax.swing JComboBox getEditor

List of usage examples for javax.swing JComboBox getEditor

Introduction

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

Prototype

public ComboBoxEditor getEditor() 

Source Link

Document

Returns the editor used to paint and edit the selected item in the JComboBox field.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> comboBox = new JComboBox<>(new String[] { "A", "B", "C" });
    comboBox.setEditable(true);//from  w  w  w.j a v a2s.c  o  m

    JTextField editorComponent = (JTextField) comboBox.getEditor().getEditorComponent();
    editorComponent.addActionListener(e -> {
        editorComponent.transferFocus();
    });

    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(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 .j av a  2 s .co  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

static JTextComponent getTextComponent(final Component component) {
    if (component instanceof JTextComponent) {
        return (JTextComponent) component;
    } else if (component instanceof JComboBox) {
        final JComboBox<?> comboBox = (JComboBox<?>) component;
        final ComboBoxEditor editor = comboBox.getEditor();
        final Component editorComponent = editor.getEditorComponent();
        return getTextComponent(editorComponent);
    } else {/*from  ww  w . ja  v a 2 s. c  om*/
        return null;
    }
}

From source file:Main.java

public static String getComboBoxValue(JComboBox comp) {
    String selectedPath = comp.getSelectedItem().toString();
    if (selectedPath == null || !selectedPath.equals(comp.getEditor().getItem())) {
        selectedPath = comp.getEditor().getItem().toString();
    }/*from  w w  w  .j  a v a 2  s . c om*/
    return selectedPath;
}

From source file:Main.java

/**
 * When the text is longer than the text box it gets centered, this
 * method ensures they all show the end of the text, eg the filename.
 *//*from  ww  w. ja  v  a  2s  .  c  om*/
public static void setCaretPosition(JComboBox comboBox) {
    Object item = comboBox.getSelectedItem();
    if (item != null) {
        String val = item.toString();
        JTextComponent c = (JTextComponent) comboBox.getEditor().getEditorComponent();
        c.setCaretPosition(val.length());
    }
}

From source file:MainClass.java

public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();

    ComboBoxEditor cbe = jcb.getEditor();

    cbe.setItem("");
}

From source file:MemComboBoxDemo.java

public MemComboAgent(JComboBox c) {
    comboBox = c;
    editor = (JTextField) c.getEditor().getEditorComponent();
    editor.addKeyListener(this);
}

From source file:Main.java

public Main() {
    JComboBox jc = new JComboBox();
    jc.addItem("France");
    jc.addItem("Germany");
    jc.addItem("Italy");
    jc.addItem("Japan");
    jc.addItemListener(this);
    add(jc);/*from  w  w w.jav  a  2 s .co m*/
    ComboBoxEditor editor = jc.getEditor();
}

From source file:king.flow.action.business.InsertCardAction.java

private void showOnComponent(int componentId, String value) {
    Component meta = (Component) getBlockMeta(componentId);
    getLogger(InsertCardAction.class.getName()).log(Level.INFO, "Display component[{0}] type : {1}",
            new Object[] { String.valueOf(meta.getId()), meta.getType().value() });
    switch (meta.getType()) {
    case LABEL://from   w w w .  ja  v a2 s.  com
        JLabel label = getBlock(meta.getId(), JLabel.class);
        label.setText(StringEscapeUtils.unescapeHtml(value));
        break;
    case TEXT_FIELD:
        JTextField textField = getBlock(meta.getId(), JTextField.class);
        textField.setText(value);
        break;
    case COMBO_BOX:
        JComboBox combo = getBlock(meta.getId(), JComboBox.class);
        if (combo.isEditable()) {
            combo.getEditor().setItem(value);
        }
        break;
    default:
        getLogger(InsertCardAction.class.getName()).log(Level.WARNING,
                "Unsupported showed component type : {0}", meta.getType());
    }
}

From source file:eu.europa.ec.markt.tlmanager.view.pages.TreeDataPublisher.java

/**
 * This method handles the state of the <code>JLabel</code>'s that are associated to mandatory fields, as well as
 * any 'special' component. It is called via two tracks: Once by the implementation of the
 * <code>BindingListener</code> in <code>BindingManager</code> and once by individual action listeners for each
 * component. These action listeners are installed at the same time of the installation of the binding (cf.
 * BindingManager.setupComponent()).//from  w w  w.j  a  v a2 s  .c om
 *
 * @param component the component
 * @param failure if the label should express a failure
 */
protected void changeMandatoryComponents(Component component, boolean failure) {
    if (mandatoryLabels == null) {
        return;
    }
    for (JLabel label : mandatoryLabels) {
        if (label == null || component == null || label.getLabelFor() == null) {
            continue;
        }
        if (label.getLabelFor().equals(component)) {
            boolean empty = false;
            if (component instanceof JTextField) {
                JTextField tf = (JTextField) component;
                if (tf.getText().isEmpty()) {
                    empty = true;
                }
            } else if (component instanceof JComboBox) {
                JComboBox box = (JComboBox) component;
                if (box.isEditable()) {
                    final String text = ((JTextComponent) box.getEditor().getEditorComponent()).getText();
                    if (StringUtils.isEmpty(text)
                            || StringUtils.equals(text, Util.DEFAULT_NO_SELECTION_ENTRY)) {
                        empty = true;
                    }
                } else {
                    if (box.getSelectedItem().equals(Util.DEFAULT_NO_SELECTION_ENTRY)) {
                        empty = true;
                    }
                }
            } else if (component instanceof JXDatePicker) {
                JXDatePicker picker = (JXDatePicker) component;
                Date date = picker.getDate();
                if (date == null) {
                    empty = true;
                }
            } else if (component instanceof DateTimePicker) {
                DateTimePicker picker = (DateTimePicker) component;
                Date date = picker.getDateTime();
                if (date == null) {
                    empty = true;
                }
            } else if (component instanceof MultivalueButton) {
                MultivalueButton button = (MultivalueButton) component;
                if (button.getMultivaluePanel().getMultivalueModel().isEmpty()) {
                    empty = true;
                }
            } else if (component instanceof CertificateProperty) {
                CertificateProperty certificateProperty = (CertificateProperty) component;
                empty = certificateProperty.isEmpty();
            }

            if (failure || empty) {
                label.setForeground(Configuration.MANDATORY_COLOR);
            } else {
                label.setForeground(Configuration.NORMAL_COLOR);
            }
            break;
        }
    }
}