简体   繁体   中英

How to Check if all the JTexFields are empty or not?

I am using a for loop to create 9 JTextFields and this is working fine. My problem is, I want to check of all the these JTextField is empty or not' at one time. I know there is a method like:

if (textbox.getText().trim().equals(""))

to check if the JTextField is empty or not, But I am not sure if it is suitable for the method that I am using for JTextField . Below is my for loop:

       for (int index = 1; index <=9; index++)
            {
                JTextField field = new JTextField(3);
                field.setPreferredSize(new Dimension(150, 50));
                field.setHorizontalAlignment(JTextField.CENTER);
                field.setEditable(false);
                second.add(field);
                second.add(Box.createHorizontalStrut(10));
                second.add(Box.createVerticalStrut(10));

            }   

Store your text fields in a List so that you can iterate over them later.

public class ClassContainingTextFields {
    private final ArrayList<JTextField> textFields = new ArrayList<>();

    // ... inside the method that creates the fields ...
        for (int i = 0; i < 9; i++) {
            JTextField field = new JTextField(3);
            //  .. do other setup 
            textFields.add(field);
        }


    /**
     * This method returns true if and only if all the text fields are empty
     **/
    public boolean allFieldsEmpty() {
        for (JTextField textbox : textFields) {
            if (! textbox.getText().trim().isEmpty() ) {
                return false;   // one field is non-empty, so we can stop immediately
            }
        }
        return true;  // every field was empty (or else we'd have stopped earlier)
    }
    // ....
}

Consider this code.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test extends JFrame implements ActionListener {
    JTextField tfs[];
    JButton btn;
    public Test(){
        setLayout( new FlowLayout());
        tfs = new JTextField[9];
        for( int i=0; i< tfs.length; i++) {
            tfs[i] = new JTextField(10);
            add(tfs[i]);
        }
        add( btn = new JButton("Check"));
        btn.addActionListener(this);
        setSize(200,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void actionPerformed (ActionEvent ae){
        boolean pass = true;
        for(int i=0; i<tfs.length; i++)
            if( tfs[i].getText().trim().equals(""))
                pass = false;

        System.out.println(pass?"Passed":"Failed");
    }
    public static void main (String args[]){
        new Test();
    }
}

How about implementing your own DocumentListener? You could have a Boolean that turns true when all fields are empty, and you could do your various hangman-related checks directly after every change in a fields Document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM