Search code examples
javajtextfieldnetbeans-8

How to jtextField focus event in NetBeas


I made a landing interface in Java using NetBeans. I want to set when the textfield input format does not meet the requirements, then Red tips occur, how to achieve this? I know it is related to focus event, but I do not know the details

enter image description here


Solution

  • I suggest that use ActionEvent and ActionListener for textfield.

    then use textfield.getText() to get String data.

    you can now examine the String's content by separate it.

    If you wanna check the format is a number or not, just use NumberFormatException.

    https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html

    after that,

    you want a red warning tip, use label with color red, and .setVisible(boolean) to control it shows or not.