0

I have 10 TextFields, organised in 2 columns of 5. I want a certain button to be disabled until all of these textfields have text written in them. I'm using Java 1.7 - all the examples I'm finding are for Java 6 (deprecated) and 8.

Can anybody walk me through this? I simply want a button to only enable after all of the textfields have been filled. Any help is highly appreciated.

MattDs17
  • 401
  • 1
  • 4
  • 20
wj1091
  • 159
  • 1
  • 2
  • 5

2 Answers2

0

The button will become enabled after the tenth text field is filled in. You have no way of knowing which text field will be the tenth, so what you likely want to do is add a change listener to each button, and within the listener check if 10 fields are non-empty. Here's a tutorial and an example.

Community
  • 1
  • 1
MattDs17
  • 401
  • 1
  • 4
  • 20
0

In java 7 you can not use lambda expressions so you have to do it in the old way.

Feel free to format the code cause was written using phone..

One fast way is to add on each TextField a change listener at it's textproperty() like this :

ChangeListener listener = new ChangeListener() {
    @Override
    public void changed(ObservableValue observable, String oldValue, String newValue) {

        boolean visible = true;

        for (TextField field : fieldsArray)
            if (field.getText().isEmpty()) {
                visible = false;
                break;
            }

        button.setVisible(visible);

    }
};
textField.textProperty().addListener(changeListener);

Remember that one listener can be added multiple times.

Something better here would be to bind Button visibleProperty() to a SimpleBooleanProperty like this:

BooleanProperty visibleProperty = new SimpleBooleanProperty();
 button.visibleProperty.bind(visibleProperty);

A little modification then in the change listener wil be done:

 ChangeListener listener = new ChangeListener(){ 
               @Override 
               public void changed(ObservableValue observable, String oldValue, String newValue) { 


                  boolean visible = true;

                  for(TextField field:fieldsArray)
                     if(field.getText().isEmpty()){
                        visible=false;
                        break;
                     }
                   visibleProperty.set(visible);  
                  } 
 };
Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93