Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GUI Enter Key Listener

Posted on 2005-04-18
5
Medium Priority
?
250 Views
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:");
          input.setEditable(true);
          while(inputtedText == null) {}
          try {
            String str = " ";
            int val = Integer.parseInt(inputtedText);
            input.setText(" ");
            input.setEditable(false);
            inputtedText = null;
            if (list.search(val))
              text.append("\n" + val + " was found in the list.");
            else
              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);
        returnInput();
      }
      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.
0
Comment
Question by:rkjohnson2005
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 13811052
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
0
 

Author Comment

by:rkjohnson2005
ID: 13811089
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 13811123
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)
0
 

Author Comment

by:rkjohnson2005
ID: 13811157
OK. I got it to work now. Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 13811651
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 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