-2

I have a view in Java where I am entering data in JTextfields. A thread is running in parallel that gets input from a keypad by using snippets of code written below. Now whenever I call

JTextField c = (JTextField) manager.getFocusOwner();
c.getText();

where the manager is

KeyboardFocusManager.getCurrentFocusManager();

It does return the text of the current JTextField but when I call the following line, it returns null.

c.getName();

Why is this happening and how should I solve this?

Kaval Patel
  • 670
  • 4
  • 20
KulaDamian
  • 154
  • 2
  • 14

1 Answers1

1

You never set a name for the text field in the first place. You can't .getName if you haven't .setName.

Cheers!

Jacob B.
  • 423
  • 3
  • 12
  • The JTextField c is just a temporary variable to get the JTextField which has the focus in the view. Do you mean that I have to use setName when creating the JTextField in the view or for the temporary var? – KulaDamian Oct 24 '17 at 11:51
  • 1
    The actual text field itself must be named with .setName. In this case it would be c.setName("Some Name"); – Jacob B. Oct 24 '17 at 11:52
  • Wait, strike that, the JTextField that is being focused must be named. – Jacob B. Oct 24 '17 at 11:55
  • Lol alright that works too an upvote or correct answer mark world be appreciated. Trying to build me rep haha – Jacob B. Oct 24 '17 at 11:57
  • Yes, I guessed that because obviously the JTextField whose focus we are requesting should set its name. :) – KulaDamian Oct 24 '17 at 11:58