Results 1 to 3 of 3

Thread: KeyListener

  1. #1
    Nh bcu is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default KeyListener

    Basically, I've created a Price checker application. Everything is working fine, I just don't know how to make it easier for my users. What I mean is that instead of click a JButton, I want them to be able to hit "enter" on their keyboard to do the same function as if you were to just click the JButton.

    Java Code:
     public void actionPerformed(ActionEvent event) {
                    if (messageBox.getText().length() < 1) {
                            // do nothing
                    } else {
                            String oldText = messageBox.getText();
                            ItemValues value = ItemValues.forValue(messageBox.getText());
                            messageBox.setText("");
                            chatBox.setText("");
                            if(value == null) {
                                    chatBox.append(oldText + ": No value found"+ "\n");
                            } else {
                                    chatBox.append(value.name().replace("_", " ") + " Value: "+value.getValue()+ "\n");
                            }
                    }
     
                    if (event.getSource() == clearMessage) {
                            messageBox.setText("");
                    }
                    if (event.getSource() == clearMessage) {
                            chatBox.setText("");
     
                           
                            
                    }
            }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: KeyListener

    Buttons have a setMnemonic(int) method which allows you to define a key press shortcut.
    The int value is one of the VK_ values in the KeyEvent class.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

Similar Threads

  1. Help with keyListener please
    By sim18 in forum AWT / Swing
    Replies: 8
    Last Post: 05-01-2013, 02:46 PM
  2. KeyListener
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2013, 07:57 PM
  3. Keylistener
    By dyelax in forum New To Java
    Replies: 4
    Last Post: 09-20-2012, 07:06 PM
  4. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 12:55 AM
  5. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM

Posting Permissions

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