Search code examples
javaswingfocusjtextfieldselectall

select all on focus in lots of jTextField


I have lots of jTextFields in my application (About 34 jTextFields) and I want all of them select all of their text when get focus and select none of text on focus lost.
Is there any way to do this with one listener or should I write a "FocusGained" and a "FocusLost" for each of these 34 jTextFields?

Thanks


Solution

  • Create on class and extend the JTextField now implement whatever you want in this class. And where you can create object of JTextField like this

    JTextField txt1 = new JTextField();
    frm.add(txt1);
    

    instead of do this way

    JTextField txt1 = new CustomText();
    frm.add(txt1);
    

    so you have to set the common class for the Text field