下面列出了javax.swing.JComboBox#getEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
private static void setSelectedItem(final JComboBox combo, final Object item) {
combo.setSelectedItem(item);
if (combo.isEditable() && combo.getEditor() != null) {
// item must be set in the editor in case of editable combobox
combo.configureEditor(combo.getEditor(), combo.getSelectedItem());
}
}
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;
}
private static void setSelectedItem(final JComboBox combo, final Object item) {
combo.setSelectedItem(item);
if (combo.isEditable() && combo.getEditor() != null) {
// item must be set in the editor in case of editable combobox
combo.configureEditor(combo.getEditor(), combo.getSelectedItem());
}
}
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;
}
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);
}