public Test() { JFrame f = new JFrame("JComboBox"); Container contentPane = f.getContentPane(); JComboBox combo = new JComboBox(fontsize); combo.setBorder(BorderFactory.createTitledBorder("请选择你要的文字大小")); combo.setEditable(true);// 将JComboBox设成是可编辑的. ComboBoxEditor editor = combo.getEditor();// getEditor()方法返回ComboBoxEditor对象,如果你查看手册,你就会发 // 现ComboBoxEditor是个接口(interface),因此你可以自行实作这个接口,制作自己想要的ComboBoxEditor组件。但通常 // 我们不需要这么做,因为默认的ComboBoxEditor是使用JTextField,这已经足够应付大部份的情况了。 // configureEditor()方法会初始化JComboBox的显示项目。例如例子中一开始就出现:"请选择或直接输入文字大小!"这个 // 字符串。 combo.configureEditor(editor, defaultMessage); contentPane.add(combo); f.pack(); f.show(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }
private void setInfos() { passwordField.setEchoChar('*'); names.setEditable(true);// 将JComboBox设成是可编辑的. ComboBoxEditor editor = names.getEditor(); names.configureEditor(editor, ""); names.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (names.getItemCount() == 0) return; if (e.getKeyCode() == KeyEvent.VK_DELETE) { nameController.deleteName((String) names.getSelectedItem()); updateNames(); repaint(); } } }); }
/** * Executes a search operation. * * @param parent Parent window used for dialogs. * @param editor Combobox editor whose color is changed depending on the search result. * @param graph Graph to search through. * @param searcher Executes the search over the graph. * @param searchString The string to search for. * @param cycleBackwards True, to cycle backwards through the results. False, to cycle in forward * order. * @param zoomToResult True, to zoom to a result. False, to move without zooming. */ public static void search(final Window parent, final ComboBoxEditor editor, final ZyGraph graph, final GraphSearcher searcher, final String searchString, final boolean cycleBackwards, final boolean zoomToResult) { // If something in the searcher changed, we have to recalculate // the search results. if (searcher.hasChanged() || !searchString.equals(searcher.getLastSearchString())) { CSearchExecuter.startNewSearch(parent, editor, graph, searcher, searchString, zoomToResult); } else if (!searcher.getResults().isEmpty()) // Don't bother cycling through an empty results // list { CSearchExecuter.cycleExistingSearch(parent, graph, searcher, cycleBackwards, zoomToResult); } }
/** * Key Listener Interface * * @param listener */ @Override public void addKeyListener(final KeyListener listener) { // // Combo Box Lookup { final ComboBoxEditor m_comboEditor = m_combo.getEditor(); final Component m_comboEditorComponent = m_comboEditor.getEditorComponent(); m_comboEditorComponent.addKeyListener(listener); } // // Text Field Lookup { m_text.addKeyListener(listener); } }
/** * Called when the combos editor changes * * @param evt * A PropertyChangeEvent object describing the event source * and the property that has changed. */ public void propertyChange(PropertyChangeEvent evt) { ComboBoxEditor newEditor = comboBox.getEditor(); if (editor != newEditor) { if (editorComponent != null) { editorComponent.removeFocusListener(this); } editor = newEditor; if (editor != null) { editorComponent = editor.getEditorComponent(); if (editorComponent != null) { editorComponent.addFocusListener(this); } } } }
public void updateMenu() { ComboBoxEditor editor = getEditor(); Object value =editor.getItem(); removeAllItems(); if(isEditable()){ if (value != null) { addItem((T)value); setSelectedIndex(0); } } List<T> items = getAvailableItems(); if (items != null) { for (T field : items) { // if (value == null || value.toString().equals(field) == false) { addItem(field); // } } } }
protected void applyContext(AbstractListBinding binding, Map context) { super.applyContext(binding, context); ComboBoxBinding comboBoxBinding = (ComboBoxBinding) binding; if (context.containsKey(RENDERER_KEY)) { comboBoxBinding.setRenderer((ListCellRenderer) decorate(context.get(RENDERER_KEY), comboBoxBinding .getRenderer())); } else if (renderer != null) { comboBoxBinding.setRenderer((ListCellRenderer) decorate(renderer, comboBoxBinding.getRenderer())); } if (context.containsKey(EDITOR_KEY)) { comboBoxBinding.setEditor((ComboBoxEditor) decorate(context.get(EDITOR_KEY), comboBoxBinding.getEditor())); } else if (editor != null) { comboBoxBinding.setEditor((ComboBoxEditor) decorate(editor, comboBoxBinding.getEditor())); } if (context.containsKey(EMPTY_SELECTION_VALUE)) { comboBoxBinding.setEmptySelectionValue(context.get(EMPTY_SELECTION_VALUE)); } else if (emptySelectionValue != null) { comboBoxBinding.setEmptySelectionValue(emptySelectionValue); } }
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 void constructPanel(String[] values) { // constructing editors editors = new PropertyValueCellEditor[types.length]; for (int i = 0; i < types.length; i++) { editors[i] = PropertyPanel.instantiateValueCellEditor(types[i], operator); } // building panel panel = new JPanel(); panel.setFocusable(true); panel.setLayout(new GridLayout(1, editors.length)); for (int i = 0; i < types.length; i++) { Component editorComponent = editors[i].getTableCellEditorComponent(null, values[i], false, 0, 0); if (editorComponent instanceof JComboBox && ((JComboBox) editorComponent).isEditable()) { if (((JComboBox) editorComponent).isEditable()) { ComboBoxEditor editor = ((JComboBox) editorComponent).getEditor(); if (editor instanceof BasicComboBoxEditor) { editor.getEditorComponent().addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } } else if (editorComponent instanceof JPanel) { JPanel editorPanel = (JPanel) editorComponent; Component[] components = editorPanel.getComponents(); for (Component comp : components) { comp.addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } panel.add(editorComponent); panel.addFocusListener(focusListener); } }
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)); } }
AnnotationSetNameComboEditor(ComboBoxEditor realEditor) { this.realEditor = realEditor; normalFont = realEditor.getEditorComponent().getFont(); italicFont = normalFont.deriveFont(Font.ITALIC); setItem(null, false); realEditor.getEditorComponent().addFocusListener(this); }
/** * Maps {@code JComboBox.configureEditor(ComboBoxEditor, Object)} * through queue */ public void configureEditor(final ComboBoxEditor comboBoxEditor, final Object object) { runMapping(new MapVoidAction("configureEditor") { @Override public void map() { ((JComboBox) getSource()).configureEditor(comboBoxEditor, object); } }); }
/** * Maps {@code JComboBox.getEditor()} through queue */ public ComboBoxEditor getEditor() { return (runMapping(new MapAction<ComboBoxEditor>("getEditor") { @Override public ComboBoxEditor map() { return ((JComboBox) getSource()).getEditor(); } })); }
/** * Maps {@code JComboBox.setEditor(ComboBoxEditor)} through queue */ public void setEditor(final ComboBoxEditor comboBoxEditor) { runMapping(new MapVoidAction("setEditor") { @Override public void map() { ((JComboBox) getSource()).setEditor(comboBoxEditor); } }); }
@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; }
/** * Return the NameTextField property value. * @return javax.swing.JTextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.JComboBox getNameTextField() { if (ivjNameTextField == null) { try { ivjNameTextField = new javax.swing.JComboBox(); ivjNameTextField.setName("NameTextField"); // user code begin {1} ivjNameTextField.setEditable(true); ComboBoxEditor editor = ivjNameTextField.getEditor(); if (editor.getEditorComponent() instanceof JTextField) { final JTextField comboBoxTextField = (JTextField)editor.getEditorComponent(); // register event listener to the text field comboBoxTextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { oKButton_ActionPerformed(e); } }); } // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } return ivjNameTextField; }
/** * Installs the UI for the given {@link JComponent}. * * @param c the JComponent to install a UI for. * * @see #uninstallUI(JComponent) */ public void installUI(JComponent c) { super.installUI(c); if (c instanceof JComboBox) { isMinimumSizeDirty = true; comboBox = (JComboBox) c; installDefaults(); popup = createPopup(); listBox = popup.getList(); // Set editor and renderer for the combo box. Editor is used // only if combo box becomes editable, otherwise renderer is used // to paint the selected item; combobox is not editable by default. ListCellRenderer renderer = comboBox.getRenderer(); if (renderer == null || renderer instanceof UIResource) comboBox.setRenderer(createRenderer()); ComboBoxEditor currentEditor = comboBox.getEditor(); if (currentEditor == null || currentEditor instanceof UIResource) { currentEditor = createEditor(); comboBox.setEditor(currentEditor); } installComponents(); installListeners(); comboBox.setLayout(createLayoutManager()); comboBox.setFocusable(true); installKeyboardActions(); comboBox.putClientProperty(BasicLookAndFeel.DONT_CANCEL_POPUP, Boolean.TRUE); } }
private void constructPanel(String[] values) { // constructing editors editors = new PropertyValueCellEditor[types.length]; for (int i = 0; i < types.length; i++) { editors[i] = PropertyPanel.instantiateValueCellEditor(types[i], operator); } // building panel panel = new JPanel(); panel.setFocusable(true); panel.setLayout(new GridLayout(1, editors.length)); for (int i = 0; i < types.length; i++) { Component editorComponent = editors[i].getTableCellEditorComponent(null, values[i], false, 0, 0); if (editorComponent instanceof JComboBox) { if (((JComboBox<?>) editorComponent).isEditable()) { ComboBoxEditor editor = ((JComboBox<?>) editorComponent).getEditor(); if (editor instanceof BasicComboBoxEditor) { editor.getEditorComponent().addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } } else if (editorComponent instanceof JPanel) { JPanel editorPanel = (JPanel) editorComponent; Component[] components = editorPanel.getComponents(); for (Component comp : components) { comp.addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } panel.add(editorComponent); panel.addFocusListener(focusListener); } }
public static JTextField getJComboBoxJTextField(JComboBox comboBox) { JTextField result = null; ComboBoxEditor cbe = comboBox.getEditor(); if (cbe != null) { Component c = cbe.getEditorComponent(); if (c instanceof JTextField) { result = (JTextField) c; } } return result; }
/** * Creates the default editor that will be used in editable combo boxes. * A default editor will be used only if an editor has not been * explicitly set with <code>setEditor</code>. * * @return a <code>ComboBoxEditor</code> used for the combo box * @see javax.swing.JComboBox#setEditor */ protected ComboBoxEditor createEditor() { BasicComboBoxEditor.UIResource bcbe = new BasicComboBoxEditor.UIResource(); if(bcbe != null) { Component c = bcbe.getEditorComponent(); if(c != null) { //把默认的Editor设置成透明(editor不透明的话就会遮住NP背景图,从而使得外观难看) ((JComponent)c).setOpaque(false); //* 以下这段是为了给默认Editor加上border而加(没有它个border将使 //* 得与不可编辑comboBox的内容组件看起来有差异哦), //* 在WindowsComboBoxUI中,这段代码是放在WindowsComboBoxEditor //* 中的方法createEditorComponent中实现,由于该 方法是1.6里才有的, //* BE LNF因要兼容java1.5,所以不作类似实现就在本方法中实现也没有问题。 //* 类似实现请参考WindowsComboBoxUI.WindowsComboBoxEditor类 // JTextField editor = (JTextField)c; Border border = (Border)UIManager.get("ComboBox.editorBorder"); if (border != null) { ((JComponent)c).setBorder(border); } } } return bcbe; }
private final JTextComponent getTextComponent() { final JComboBox<?> comboBox = getComboBox(); final ComboBoxEditor comboEditor = comboBox == null ? null : comboBox.getEditor(); final Component comboEditorComponent = comboEditor == null ? null : comboEditor.getEditorComponent(); if (comboEditorComponent instanceof JTextComponent) { return (JTextComponent)comboEditorComponent; } return null; }
private final synchronized void configureEditor() { if (_configureEditorRunning) { return; } _configureEditorRunning = true; try { if (_editorComp != null) { // destroy the old editor component in order to create a new one destroyEditorComponent(); } final ComboBoxEditor newEditor = comboBox.getEditor(); _editorComp = (JTextComponent)(newEditor == null ? null : newEditor.getEditorComponent()); if (_editorComp != null) { _editorComp.addKeyListener(editorKeyListener); _editorComp.addFocusListener(editorFocusListener); _editorComp.setDocument(ComboBoxAutoCompletion.this); _editorComp.setInputVerifier(editorInputVerifier); } } finally { _configureEditorRunning = false; } }
@Override protected ComboBoxEditor createEditor() { // In case the combo box is editable we inherit the size variant to the editor. // TODO this needs to be done later to support client property override // because the editor is created in the ComboBox constructor already a later set of the property has no effect SynthComboBoxEditor result = new SynthComboBoxEditor(); String scaleKey = SeaGlassStyle.getSizeVariant(comboBox); if (scaleKey != null) { ((JComponent)result.getEditorComponent()).putClientProperty("JComponent.sizeVariant", scaleKey); } return result; }
public void addEditor() { ComboBoxEditor cbe = comboBox.getEditor(); if (cbe == null) return; editor = cbe.getEditorComponent(); if (editor == null) return; configureEditor(); comboBox.add(editor); }
private void constructPanel(String[] values) { // constructing editors editors = new PropertyValueCellEditor[types.length]; for (int i = 0; i < types.length; i++) { editors[i] = PropertyPanel.instantiateValueCellEditor(types[i], operator); } // building panel panel = new JPanel(); panel.setFocusable(true); panel.setLayout(new GridLayout(1, editors.length)); for (int i = 0; i < types.length; i++) { Component editorComponent = editors[i].getTableCellEditorComponent(null, values[i], false, 0, 0); if (editorComponent instanceof JComboBox && ((JComboBox) editorComponent).isEditable()) { if ( ((JComboBox) editorComponent).isEditable() ) { ComboBoxEditor editor = ((JComboBox) editorComponent).getEditor(); if (editor instanceof BasicComboBoxEditor) { editor.getEditorComponent().addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } } else if (editorComponent instanceof JPanel) { JPanel editorPanel = (JPanel) editorComponent; Component[] components = editorPanel.getComponents(); for (Component comp : components) { comp.addFocusListener(focusListener); } } else { editorComponent.addFocusListener(focusListener); } panel.add(editorComponent); panel.addFocusListener(focusListener); } }
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"); } }