Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all textfield of jframe using loop?

I'm developing Java application using NetBeans. I have 5 JTextFields and 2 JTextArea in JFrame. I want to clear them at once using a loop. How can it be done?

like image 992
Pradip Kharbuja Avatar asked Nov 22 '25 07:11

Pradip Kharbuja


1 Answers

Iterate over all of the components and set the text of all JTextField and JTextArea objects to an empty String:

//Note: "this" should be the Container that directly contains your components
//(most likely a JPanel).
//This won't work if you call getComponents on the top-level frame.
for (Component C : this.getComponents())
{    
    if (C instanceof JTextField || C instanceof JTextArea){

        ((JTextComponent) C).setText(""); //abstract superclass
    }
}
like image 154
ApproachingDarknessFish Avatar answered Nov 24 '25 23:11

ApproachingDarknessFish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!