GUI Enter Key Listener

Posted on 2005-04-18
Last Modified: 2010-03-31
I'm having issues with my first GUI program.  I have a JTextArea set up for input and it's giving me problems.  I've tried a few methods for getting the input and it seems to lock up the computer.

Here is the problem piece of code:

Message is a JLabel and text is a JTextArea.  This is inside an actionPerformed method for the class.  action = e.getActionCommand();

        else if(action.equals("search(int)")) {
          message.setText("Enter the value to search for:");
          while(inputtedText == null) {}
          try {
            String str = " ";
            int val = Integer.parseInt(inputtedText);
            input.setText(" ");
            inputtedText = null;
            if (
              text.append("\n" + val + " was found in the list.");
              text.append("\n" + val + " was not found in the list.");
          catch(Exception e) {
            text.append("\nInput error. Please try the method again.");

Here is a KeyListener I set up to complement this:

    private class TextListener implements KeyListener {
      public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_ENTER);
      public void keyTyped(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
      public void returnInput() {
        inputtedText = input.getText();

Essentially, I want to do this within the actionPerformed:
-Change the message to prompt for input
-wait for input
-read the input from the user after they have pushed the ENTER key.
-pass the input to the search() method

Any help here would be hot.
Question by:rkjohnson2005
    LVL 92

    Accepted Solution

    you can't wait for input in your actionPerformed(), thats what is blocking your gui.

    Instead add an action listener to your text field. It will then get called when the user hits enter

    Author Comment

    Is there a way to define the action listener on the text field so that it knows which method (in this case "search(int)") was just called?
    LVL 92

    Expert Comment

    No, you'll need to set some flag in your class to mark that.
    (Or I guess you could check the label as that currently gets set)

    Author Comment

    OK. I got it to work now. Thanks.
    LVL 92

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now