JAVA - How To Clear All JTextfield Text In Java

JAVA - How To Remove All JTextfield Text In Java Netbeans

In This Java Tutorial We will See How To Remove Text From All TextField In A Jframe Using For Loop On A JButton Click Event In NetBean IDE.


Project Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Project extends JFrame{

    // create a table of  JTextFieldes
    JTextField[] jtf = {new JTextField(),new JTextField(),new JTextField(),new JTextField(),
                        new JTextField(),new JTextField(),new JTextField(),new JTextField(),
                         new JTextField()};

 //button to clear all textboxes
    JButton btn;

public Project(){
    
//create a loop too add all jtextfieldes
    int j = 10;//for change the place of jtextfieldes
    for(int i = 0; i < jtf.length; i++){
        jtf[i].setBounds(10, j, 100, 20);
        add(jtf[i]);
        j += 30;
    }
    
    btn = new JButton("clear text");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
        
          //create a loop to remove all jtextfieldes text
            for(int i = 0; i < jtf.length; i++){
                jtf[i].setText("");
            }
        }
    });
    
    btn.setBounds(180, 140, 150, 20);
    add(btn);
    
    
    setLayout(null);
    setSize(450, 350);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.decode("#bdb76b"));
    setVisible(true);
}

public static void main(String[] args){
   
new Project();
}
}
///////////////////// END :)
//////////////OUTPUT:
java clear all jtexfields text




Share this

Related Posts

Previous
Next Post »