List of utility methods to do JComboBox Event
void | addActionListener(ActionListener listener, JComboBox... components) add Action Listener for (JComboBox component : components) {
component.addActionListener(listener);
|
void | addActionListeners(JComboBox comboBox, ActionListener[] listeners) add Action Listeners for (ActionListener listener : listeners) {
comboBox.addActionListener(listener);
|
JComboBox | createComboBox(String actionCommand, ActionListener listener, String... items) create Combo Box JComboBox box = new JComboBox(); box.setEditable(false); for (String item : items) { box.addItem(item); if (listener != null) { box.addActionListener(listener); box.setActionCommand(actionCommand); ... |
JComboBox | createComboBox(TYPE[] items, ActionListener... listeners) create Combo Box JComboBox<TYPE> result = new JComboBox<>(items); for (ActionListener listener : listeners) { result.addActionListener(listener); return result; |
void | installComboBoxCopyAction(JComboBox comboBox) install Combo Box Copy Action final Component editorComponent = comboBox.getEditor().getEditorComponent(); if (!(editorComponent instanceof JTextComponent)) return; final InputMap inputMap = ((JTextComponent) editorComponent).getInputMap(); for (KeyStroke keyStroke : inputMap.allKeys()) { if (DefaultEditorKit.copyAction.equals(inputMap.get(keyStroke))) { comboBox.getInputMap().put(keyStroke, DefaultEditorKit.copyAction); comboBox.getActionMap().put(DefaultEditorKit.copyAction, new AbstractAction() { @Override public void actionPerformed(final ActionEvent e) { if (!(e.getSource() instanceof JComboBox)) return; final JComboBox comboBox = (JComboBox) e.getSource(); final String text; final Object selectedItem = comboBox.getSelectedItem(); if (selectedItem instanceof String) { text = (String) selectedItem; } else { final Component component = comboBox.getRenderer().getListCellRendererComponent(new JList(), selectedItem, 0, false, false); if (component instanceof JLabel) { text = ((JLabel) component).getText(); } else if (component != null) { final String str = component.toString(); text = str == null || str.startsWith(component.getClass().getName() + "[") ? null : str; } else { text = null; if (text != null) { final JTextField textField = new JTextField(text); textField.selectAll(); textField.copy(); }); |
void | setAction(JComboBox cmb, ActionListener listener, String actionCommand) set Action cmb.addActionListener(listener); cmb.setActionCommand(actionCommand); |