Results 1 to 5 of 5
  1. #1
    beatzz is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default Displaying a JComboBox's model content on KeyPressed

    I would like my JComboBox named cbxType, while in focus and having been pressed 'Enter' upon, to behave the same way it does when it is clicked on by a mouse. When you click on it with a mouse, it drops down the a list of all the model content. When I have it in focus, and press 'Enter' it does nothing. So, I have created an event handler, cbxTypeKeyPressed.
    Java Code:
    private void cbxTypeKeyPressed(java.awt.event.KeyEvent evt) {                                   
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                // Display Model!!!!
            }
        }
    I have read the javadoc for the JComboBox class, searched online, requested help on IRC, to no avail. I have tried inseting cbxType.getModel(), cbxType.showPopup() which both seem like common sense choices, but neither work.

    PS - the Conditional statement evt.getKeyCode() == KeyEvent.VK_ENTER in the event handler is not the issue. It works for other event handlers I have.
    Last edited by beatzz; 10-20-2017 at 10:50 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Displaying a JComboBox's model content on KeyPressed

    You need to wrap this in a SwingUtilities.invokeLater call. Mind that you also need to check for visibility, otherwise the selection with enter doesn't work properly:
    Java Code:
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
       if (!comboBox.isPopupVisible()) {
           SwingUtilities.invokeLater(comboBox::showPopup);
       }
    }
    NB: This example uses Java 8 lambda syntax for the Runnable. If you're not on Java 8 (but you really should be...) then this is your code:
    Java Code:
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
       if (!comboBox.isPopupVisible()) {
           SwingUtilities.invokeLater(new Runnable() {
           @Override
              public void run() {
                    comboBox.showPopup();
              }
          });
       }
    }
    Last edited by SurfMan; 10-20-2017 at 11:34 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Displaying a JComboBox's model content on KeyPressed

    Can you show the code that adds the keyListener and the full code for the keyReleased method?

  4. #4
    beatzz is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default Re: Displaying a JComboBox's model content on KeyPressed

    SurfMan: thank you, your example worked!! And I'm definitely on Java-8 ;) I don't understand the solution, but I will research it and learn from it, thank you!
    Benji2505:
    Java Code:
    cbxType.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    cbxTypeItemStateChanged(evt);
                }
            });
            cbxType.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    cbxTypeKeyPressed(evt);
                }
            });
    Also, there is no keyReleased method.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Displaying a JComboBox's model content on KeyPressed

    Although this is not related to your post, you might also want to consider using import statements. They can make your coding experience more pleasant (with an occasional but rare annoyance). See Package Members
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Displaying The Content of Doc files in Web Browser
    By mitesh1984_agrawal in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2010, 08:41 AM
  2. JComboBox displaying variable two times, why?
    By ecliptical in forum New To Java
    Replies: 6
    Last Post: 01-24-2010, 06:09 PM
  3. Dynamic data model in JComboBox
    By agreed in forum AWT / Swing
    Replies: 5
    Last Post: 11-15-2009, 08:30 PM
  4. Problem displaying the view content.
    By poonam2008 in forum Eclipse
    Replies: 0
    Last Post: 11-30-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •