Java Code Examples for javax.swing.JComboBox#addPropertyChangeListener()

The following examples show how to use javax.swing.JComboBox#addPropertyChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericPanelBuilder.java    From Ardulink-2 with Apache License 2.0 6 votes vote down vote up
private static JComponent createComboxBox(final ConfigAttribute attribute) {
	final JComboBox jComboBox = new JComboBox(attribute.getChoiceValues());
	jComboBox.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			attribute.setValue(jComboBox.getSelectedItem());
		}
	});
	final boolean nullIsAvalidItem = Arrays.asList(
			attribute.getChoiceValues()).contains(null);
	// raise a selection event on model changes
	jComboBox.addPropertyChangeListener("model",
			new PropertyChangeListener() {
				@Override
				public void propertyChange(PropertyChangeEvent pce) {
					setSelection(jComboBox, attribute.getValue(),
							nullIsAvalidItem);
				}
			});
	setSelection(jComboBox, attribute.getValue(), nullIsAvalidItem);
	return jComboBox;
}
 
Example 2
Source File: ComboBoxCellEditor.java    From cropplanning with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new ComboBoxCellEditor.
 * @param comboBox the comboBox that should be used as the cell editor.
 */
public ComboBoxCellEditor(final JComboBox comboBox) {
    this.comboBox = comboBox;
    
    handler = new Handler();
    
    // Don't do this:
    // this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
    // it probably breaks various things
    
    // hitting enter in the combo box should stop cellediting
    JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
    editorComponent.addKeyListener(handler);
    // remove the editor's border - the cell itself already has one
    editorComponent.setBorder(null);

    // editor component might change (e.g. a look&feel change)
    // the new editor component needs to be modified then (keyListener, border)
    comboBox.addPropertyChangeListener(handler);
}
 
Example 3
Source File: SeaGlassComboBoxUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private EditorFocusHandler(JComboBox comboBox) {
    this.comboBox = comboBox;
    editor = comboBox.getEditor();
    if (editor != null) {
        editorComponent = editor.getEditorComponent();
        if (editorComponent != null) {
            editorComponent.addFocusListener(this);
        }
    }
    comboBox.addPropertyChangeListener("editor", this);
}
 
Example 4
Source File: AutoCompleteDecorator.java    From cropplanning with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Enables automatic completion for the given JComboBox. The automatic
 * completion will be strict (only items from the combo box can be selected)
 * if the combo box is not editable.
 * @param comboBox a combo box
 * @param stringConverter the converter used to transform items to strings
 */
public static void decorate(final JComboBox comboBox, final ObjectToStringConverter stringConverter) {
    boolean strictMatching = !comboBox.isEditable();
    // has to be editable
    comboBox.setEditable(true);
    // fix the popup location
    AquaLnFPopupLocationFix.install(comboBox);

    // configure the text component=editor component
    JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
    final AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(comboBox);
    final AutoCompleteDocument document = new AutoCompleteDocument(adaptor, strictMatching, stringConverter);
    decorate(editorComponent, document, adaptor);
    
    // show the popup list when the user presses a key
    final KeyListener keyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            // don't popup on action keys (cursor movements, etc...)
            if (keyEvent.isActionKey()) return;
            // don't popup if the combobox isn't visible anyway
            if (comboBox.isDisplayable() && !comboBox.isPopupVisible()) {
                int keyCode = keyEvent.getKeyCode();
                // don't popup when the user hits shift,ctrl or alt
                if (keyCode==keyEvent.VK_SHIFT || keyCode==keyEvent.VK_CONTROL || keyCode==keyEvent.VK_ALT) return;
                // don't popup when the user hits escape (see issue #311)
                if (keyCode==keyEvent.VK_ESCAPE) return;
                comboBox.setPopupVisible(true);
            }
        }
    };
    editorComponent.addKeyListener(keyListener);
    
    if (stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
        comboBox.setEditor(new AutoCompleteComboBoxEditor(comboBox.getEditor(), stringConverter));
    }
    
    // Changing the l&f can change the combobox' editor which in turn
    // would not be autocompletion-enabled. The new editor needs to be set-up.
    comboBox.addPropertyChangeListener("editor", new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
          	ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue();
          	if (editor!=null && editor.getEditorComponent()!=null) {
                if (!(editor instanceof AutoCompleteComboBoxEditor) 
                    && stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
                    comboBox.setEditor(new AutoCompleteComboBoxEditor(editor, stringConverter));
                    // Don't do the decorate step here because calling setEditor will trigger
                    // the propertychange listener a second time, which will do the decorate
                    // and addKeyListener step.
                } else {
                    decorate((JTextComponent) editor.getEditorComponent(), document, adaptor);
                    editor.getEditorComponent().addKeyListener(keyListener);
                }
          	}
        }
    });
}