Search code examples
javaswingjtextfieldjtextarea

How to keep Focus on JTextField while clicking input number buttons?


I am new to GUI stuff and am having trouble with the following problem. I have 3 JTextFields, credit card number, expiration date, and security number. I am able to input information into the fields. I also implemented the focus listener for each button. If I click it, it says gained focus, if I click anywhere else, it loses focus. Under these text fields, I have a number pad (touch screen/mouse click) to enter the numbers. How do I keep focus on that particular text field until ONLY and SPECIFICALLY one of the other two textfields are clicked? The textfield that currently has focus will lose focus once I try to click to input numbers. I don't want this to happen. I searched online and wasn't able to find something specific to my case. Any help or tips would be appreciated.


Solution

  • myJButton.setFocusable(false);
    

    or if a bunch of buttons held in an allMyButtons collection:

    for (JButton button: allMyButtons) {
        button.setFocusable(false);
    }
    

    That's it.