Java Code Examples for javax.swing.JComboBox#isPopupVisible()

The following examples show how to use javax.swing.JComboBox#isPopupVisible() . 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
Source File: FindDialog.java    From FancyBing with 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 2
Source File: bug8133039.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean accept(Object sender) {
    ACTION_ACCEPTED_CALLS++;
    if (sender instanceof JComboBox) {
        JComboBox c = (JComboBox) sender;
        return !c.isPopupVisible();
    }
    return false;
}
 
Example 3
Source File: EmbeddedFrameGrabTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test fails if it throws any exception.
 *
 * @throws Exception
 */
private void init() throws Exception {

    if (!System.getProperty("os.name").startsWith("Windows")) {
        System.out.println("This is Windows only test.");
        return;
    }

    final Frame frame = new Frame("AWT Frame");
    frame.pack();
    frame.setSize(200, 200);
    FramePeer frame_peer = AWTAccessor.getComponentAccessor()
                                .getPeer(frame);
    Class comp_peer_class
            = Class.forName("sun.awt.windows.WComponentPeer");
    Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
    hwnd_field.setAccessible(true);
    long hwnd = hwnd_field.getLong(frame_peer);

    Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
    Constructor constructor
            = clazz.getConstructor(new Class[]{long.class});
    final Frame embedded_frame
            = (Frame) constructor.newInstance(new Object[]{
                new Long(hwnd)});;
    final JComboBox<String> combo = new JComboBox<>(new String[]{
        "Item 1", "Item 2"
    });
    combo.setSelectedIndex(1);
    final Panel p = new Panel();
    p.setLayout(new BorderLayout());
    embedded_frame.add(p, BorderLayout.CENTER);
    embedded_frame.validate();
    p.add(combo);
    p.validate();
    frame.setVisible(true);
    Robot robot = new Robot();
    robot.delay(2000);
    Rectangle clos = new Rectangle(
            combo.getLocationOnScreen(), combo.getSize());
    robot.mouseMove(clos.x + clos.width / 2, clos.y + clos.height / 2);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(1000);
    if (!combo.isPopupVisible()) {
        throw new RuntimeException("Combobox popup is not visible!");
    }
    robot.mouseMove(clos.x + clos.width / 2, clos.y + clos.height + 3);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(1000);
    if (combo.getSelectedIndex() != 0) {
        throw new RuntimeException("Combobox selection has not changed!");
    }
    embedded_frame.remove(p);
    embedded_frame.dispose();
    frame.dispose();

}
 
Example 4
Source File: AutoCompleteDecorator.java    From cropplanning with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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);
                }
          	}
        }
    });
}