下面列出了javax.swing.JComboBox#addPropertyChangeListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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);
}
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);
}
/**
* 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);
}
}
}
});
}