Java Code Examples for javax.swing.ComboBoxEditor

The following examples show how to use javax.swing.ComboBoxEditor. These examples are extracted from open source projects. 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
/**
 * 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);
	}
}
 
Example 2
Source Project: FlatLaf   Source File: FlatComboBoxUI.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source Project: binnavi   Source File: CSearchExecuter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 4
Source Project: seaglass   Source File: SeaGlassComboBoxUI.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 5
Source Project: FancyBing   Source File: FindDialog.java    License: GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 6
Source Project: netbeans   Source File: ComboBoxAutoCompleteSupport.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source Project: netbeans   Source File: ComboBoxAutoCompleteSupport.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
AnnotationSetNameComboEditor(ComboBoxEditor realEditor) {
  this.realEditor = realEditor;
  normalFont = realEditor.getEditorComponent().getFont();
  italicFont = normalFont.deriveFont(Font.ITALIC);
  setItem(null, false);
  realEditor.getEditorComponent().addFocusListener(this);
}
 
Example 9
/**
 * 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);
        }
    });
}
 
Example 10
/**
 * Maps {@code JComboBox.getEditor()} through queue
 */
public ComboBoxEditor getEditor() {
    return (runMapping(new MapAction<ComboBoxEditor>("getEditor") {
        @Override
        public ComboBoxEditor map() {
            return ((JComboBox) getSource()).getEditor();
        }
    }));
}
 
Example 11
/**
 * 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);
        }
    });
}
 
Example 12
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);
	}
}
 
Example 13
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");
	}
}
 
Example 14
@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));
	}
}
 
Example 15
Source Project: beautyeye   Source File: BEComboBoxUI.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * 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;
    }
 
Example 16
Source Project: seaglass   Source File: SeaGlassComboBoxUI.java    License: Apache License 2.0 5 votes vote down vote up
@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;

}
 
Example 17
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);
        }
    }
}
 
Example 18
Source Project: jdal   Source File: AutoCompletionListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void keyReleased(KeyEvent e) {
	char ch = e.getKeyChar();
	if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
		return;
	
	ComboBoxEditor editor = (ComboBoxEditor) combo.getEditor();
	String editing =  ((JTextField) editor.getEditorComponent()).getText();
	combo.removeAllItems();
	addList(getList(editing));
	combo.setPopupVisible(true);
	((JTextField) editor.getEditorComponent()).setText(editing);
	combo.repaint();
}
 
Example 19
Source Project: dragonwell8_jdk   Source File: Test8015300.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
 
Example 20
Source Project: TencentKona-8   Source File: Test8015300.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
 
Example 21
Source Project: jdk8u60   Source File: Test8015300.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
 
Example 22
Source Project: openjdk-jdk8u   Source File: Test8015300.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
 
Example 23
Source Project: netbeans   Source File: ReturnTypeUIHelper.java    License: Apache License 2.0 4 votes vote down vote up
ReturnTypeComboBoxEditor(ComboBoxEditor delegate) {
    this.delegate = delegate;
}
 
Example 24
Source Project: netbeans   Source File: MdbLocationPanelVisual.java    License: Apache License 2.0 4 votes vote down vote up
public ProjectDestinationsComboModel(ComboBoxEditor comboEditor) {
    this.comboEditor = comboEditor;
}
 
Example 25
Source Project: netbeans   Source File: DatasourceUIHelper.java    License: Apache License 2.0 4 votes vote down vote up
DatasourceComboBoxEditor(ComboBoxEditor delegate) {
    this.delegate = delegate;
}
 
Example 26
Source Project: netbeans   Source File: RuntimeComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void setEditor(ComboBoxEditor anEditor) {
    combo.setEditor(anEditor);
}
 
Example 27
Source Project: netbeans   Source File: RuntimeComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void configureEditor(ComboBoxEditor anEditor, Object anItem) {
    combo.configureEditor(anEditor, anItem);
}
 
Example 28
Source Project: netbeans   Source File: Repository.java    License: Apache License 2.0 4 votes vote down vote up
private UrlComboBoxEditor(ComboBoxEditor originalEditor) {
    this.origEditor = originalEditor;
    ((JTextComponent) originalEditor.getEditorComponent())
            .getDocument().addDocumentListener(this);
}
 
Example 29
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}
 
Example 30
Source Project: openjdk-jdk9   Source File: Test8015300.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info : array) {
        UIManager.setLookAndFeel(info.getClassName());
        System.err.println("L&F: " + info.getName());
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                combo = new JComboBox<>(ITEMS);
                combo.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent event) {
                        if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
                            ComboBoxEditor editor = combo.getEditor();
                            Object component = editor.getEditorComponent();
                            if (component instanceof JTextField) {
                                JTextField text = (JTextField) component;
                                boolean selected = null != text.getSelectedText();

                                StringBuilder sb = new StringBuilder();
                                sb.append(" - ").append(combo.getSelectedIndex());
                                sb.append(": ").append(event.getItem());
                                if (selected) {
                                    sb.append("; selected");
                                }
                                System.err.println(sb);
                                if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
                                    throw new Error("unexpected state of text selection");
                                }
                            }
                        }
                    }
                });
                JFrame frame = new JFrame(getClass().getSimpleName());
                frame.add(combo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
        for (int i = 0; i < ITEMS.length; ++i) {
            select(i, true);
            select(1, false);
        }
        invokeAndWait(new Runnable() {
            @Override
            public void run() {
                windowForComponent(combo).dispose();
            }
        });
    }
}