Search code examples
javajtextfieldfocusmanagerkeyboardfocusmanager

How to find the name of current JTextField of a View in Java?


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?


Solution

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

    Cheers!