下面列出了javax.swing.ComboBoxEditor#getEditorComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected ComboBoxEditor createEditor() {
ComboBoxEditor comboBoxEditor = super.createEditor();
Component editor = comboBoxEditor.getEditorComponent();
if( editor instanceof JTextField ) {
JTextField textField = (JTextField) editor;
textField.setColumns( editorColumns );
// assign a non-null and non-javax.swing.plaf.UIResource border to the text field,
// otherwise it is replaced with default text field border when switching LaF
// because javax.swing.plaf.basic.BasicComboBoxEditor.BorderlessTextField.setBorder()
// uses "border instanceof javax.swing.plaf.basic.BasicComboBoxEditor.UIResource"
// instead of "border instanceof javax.swing.plaf.UIResource"
textField.setBorder( BorderFactory.createEmptyBorder() );
}
return comboBoxEditor;
}
/**
* Initializes the ComboBoxInput variable
*
* @param ignored not used
*/
private void initComboBoxEditor(Object... ignored) {
ComboBoxEditor editor = comboBox.getEditor();
if (editor == null) {
return;
}
if (editor.getEditorComponent() instanceof JTextComponent) {
if (comboBoxInput != null) {
//Clear old listener
comboBoxInput.getDocument().removeDocumentListener(this);
comboBoxInput.removeFocusListener(focusChangeListener);
comboBoxInput.removeKeyListener(updateOnEnterKey);
}
comboBoxInput = ((JTextComponent) editor.getEditorComponent());
comboBoxInput.getDocument().addDocumentListener(this);
// workaround for java bug #6433257
comboBoxInput.addFocusListener(focusChangeListener);
// allow enter to use current value
comboBoxInput.addKeyListener(updateOnEnterKey);
}
}
@SuppressWarnings("unchecked")
private JPanel createInputPanel()
{
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel = new JPanel(new BorderLayout());
m_comboBox = new JComboBox(getHistory().toArray());
StringBuilder prototype = new StringBuilder(70);
for (int i = 0; i < 40; ++i)
prototype.append('-');
m_comboBox.setPrototypeDisplayValue(prototype.toString());
m_comboBox.setEditable(true);
ComboBoxEditor editor = m_comboBox.getEditor();
m_comboBox.addActionListener(this);
m_textField = (JTextField)editor.getEditorComponent();
m_textField.selectAll();
KeyListener keyListener = new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if (c == KeyEvent.VK_ESCAPE
&& ! m_comboBox.isPopupVisible())
dispose();
}
};
m_textField.addKeyListener(keyListener);
GuiUtil.setMonospacedFont(m_comboBox);
innerPanel.add(m_comboBox, BorderLayout.CENTER);
outerPanel.add(innerPanel, BorderLayout.NORTH);
return outerPanel;
}
public static boolean install( JComboBox combo ) {
boolean res = false;
ComboBoxEditor comboEditor = combo.getEditor();
if( comboEditor.getEditorComponent() instanceof JTextComponent ) {
JTextComponent textEditor = ( JTextComponent ) comboEditor.getEditorComponent();
Document doc = textEditor.getDocument();
doc.addDocumentListener( new AutoCompleteListener( combo ) );
setIgnoreSelectionEvents( combo, false );
combo.setEditable( true );
res = true;
}
combo.putClientProperty( "nb.combo.autocomplete", res ); //NOI18N
return res;
}
public static boolean install( JComboBox combo ) {
boolean res = false;
ComboBoxEditor comboEditor = combo.getEditor();
if( comboEditor.getEditorComponent() instanceof JTextComponent ) {
JTextComponent textEditor = ( JTextComponent ) comboEditor.getEditorComponent();
Document doc = textEditor.getDocument();
doc.addDocumentListener( new AutoCompleteListener( combo ) );
setIgnoreSelectionEvents( combo, false );
combo.setEditable( true );
res = true;
}
combo.putClientProperty( "nb.combo.autocomplete", res ); //NOI18N
return res;
}
public AutoCompletionComboBoxEditor(ComboBoxEditor editor) {
if ((editor.getEditorComponent() instanceof JTextField)) {
this.editor = editor;
editorComponent = (JTextField) editor.getEditorComponent();
editorComponent.getDocument().addDocumentListener(docListener);
editorComponent.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
setSelectedItem(editorComponent.getText());
actionPerformed(new ActionEvent(this, 0, "editingStoped"));
e.consume();
} else if (e.getKeyCode() == KeyEvent.VK_TAB) {
if (isPopupVisible()) {
hidePopup();
} else {
showPopup();
}
e.consume();
} else {
super.keyPressed(e);
}
}
});
} else {
throw new IllegalArgumentException("Only JTextField allowed as editor component");
}
}
@Override
public void setEditor(ComboBoxEditor anEditor) {
// check if editor component has changed at all: Otherwise listener already registered
if (getEditor() == null || anEditor.getEditorComponent() != getEditor().getEditorComponent()) {
super.setEditor(new AutoCompletionComboBoxEditor(anEditor));
}
}
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals("editor")) {
ComboBoxEditor editor = comboBox.getEditor();
if (editor!=null && editor.getEditorComponent()!=null) {
JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
editorComponent.addKeyListener(this);
editorComponent.setBorder(null);
}
}
}