0

I have a JTextField in my program which I hooked up a Keyboard Listener to through the use of an anonymous inner class. the listener clears the text box and saves the word currently on it.

I want to be able to use that word I got out of it in other parts of the code but I know all variables used in inner classes have to be tagged final.. so how do I do this?

here's my simplified code to give you guys an idea - I want to be able to use userWord

    typingArea.addKeyListener(new KeyAdapter() {
        public void keyPressed (KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) { // enter key is pressed
                userWord = typingArea.getText().toLowerCase();
                typingArea.setText("");

            }
        }
    });

EDIT: Just had the idea of maybe passing it as a constructor variable to another object I can create which would then be able to extract and save that string.. would this work? Sorry for the pointless question if I literally thought of a solution a second after asking, haha.

gadu
  • 1,816
  • 1
  • 17
  • 31

1 Answers1

2
  1. Use an ActionListener instead of a KeyListener and KeyEvent.VK_ENTER - lots of reasons why, but basically, this is what an ActionListener was designed to do.
  2. Use a class field instead of a local variable...

More like...

public class MyForm extends JPanel {

    private JTextField typingArea;
    private String userWord; 

    public MyForm() {

        typingArea = new JTextField();
        typingArea.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                userWord = typingArea.getText().toLowerCase();
                typingArea.setText("");
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I originally used a class field - but it had to be tagged final which didn't allow it to be edited, so I wrote it this way because using a class field won't even compile - and I'm not sure how I would be able to use an ActionListener to determine if Enter's been pressed or why that would be better for a such a simple operation? – gadu Apr 24 '13 at 03:48
  • 1
    1- So, don't tag it `final`. 2- `ActionListener` is used to provide a platform independent mechanism for "accept" actions. Not all platforms may use "Enter" as there accept key. Better to try and meet the expectations of the end user then invent new ones – MadProgrammer Apr 24 '13 at 03:51
  • `userWord = typingArea.getText().toLowerCase();` will give a compile error saying userWord must be tagged final if I omit it. I suppose I could create a whole entire new class like you suggested but I was just wondering if there was a way to extract data from an anonymous inner class without having to create a new class – gadu Apr 24 '13 at 03:58
  • Did you declare it as a class field? It sounds like the variable is still declared as a local variable. See my example? The `userWord` variable appears after the class deceleration but out side any methods or constructors – MadProgrammer Apr 24 '13 at 04:01
  • I'm an idiot... I was declaring it in the constructor - forgot the whole point of inner classes is so they'd have access to the same class fields... thanks again for the help! one last question though - how do I hook up an action listener to fire the actionPerformed on having the enter key pressed.. you're current implementation just seems like there's nothing to trigger the ActionEvent – gadu Apr 24 '13 at 04:19
  • As I stated, this is what `ActionListener`s do by default. The `JTextField` is designed to trigger the `actionPerformed` method when a platform dependent "accept" event occurs (or on most systems, "Enter" is pressed) – MadProgrammer Apr 24 '13 at 04:22
  • +1 for patience. `you're current implementation just seems like there's nothing to trigger the ActionEvent` - It's mind boggling that you post code and the OP can't even try the posted code. I mean why did he think you took the time to post the code? Its much faster to copy/paste/compile/test then it is a ask a question and wait/hope somebody replies. – camickr Apr 24 '13 at 04:42
  • I tried the code and it worked - I was just wondering why. I want to understand how these things work for the future - sorry! – gadu Apr 25 '13 at 20:18
  • Essentially, the UI delegate registers a key listener that monitors for the "accept" key. When triggered, it fires an ActionPerformed event to all registered listeners. The same thing happens for JButtons – MadProgrammer Apr 25 '13 at 20:20