4

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));

            }   
mKorbel
  • 109,525
  • 20
  • 134
  • 319
CrazyPixi
  • 95
  • 3
  • 7
  • I don't understand, when do you need to check if they are empty? – Djon Jun 01 '13 at 22:04
  • Actually, I am making a game "Hangman" , so when the user guess the word all of the JTextfield will be fill by the letters, then it will check if there is no JTextfield is empty, User's Win the game.. – CrazyPixi Jun 01 '13 at 22:16

3 Answers3

7

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)
    }
    // ....
}
Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99
  • Is it possible to do it, like if all the Jtextfield is having some character in it, then stop the program, actually I am making a hangman game in which user Enter letters in JTexField. So, when the user guessed the word, all the JTexField is filled with letter, than I want my program to check, if evry field is filled with characters, then it will say, user's win..!! – CrazyPixi Jun 01 '13 at 22:58
  • You mean like `textbox.getText().indexOf('n')` to test if there's an 'n' in the text field? Sure. Anything you can do to one field you can do to all of them. – Nathaniel Waisbrot Jun 01 '13 at 23:34
1

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();
    }
}
laksys
  • 3,228
  • 4
  • 27
  • 38
1

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.

Jannis Alexakis
  • 1,279
  • 4
  • 19
  • 38