Вопрос

I was lazy and i decided to add bunch of listeners onto my controls in loop.Bud i noticed that when i use fxml variable from field

@FXML
private TextField profileNameField = new TextField();

and add handler to it

profileNameField .setOnKeyReleased(new CustomEventHandler(profileNameField, l1) {

            Pattern pattern = Pattern.compile(regexes[0],
                    Pattern.DOTALL);

            Matcher matcher;

            @Override
            public void handle(Event event) {

                System.out.println("handling");
                String text = this.control.getText();
                matcher = pattern.matcher(text);

                if (matcher.matches()) {
                    verifyed();
                } else {
                    failedToVerify();
                }
            }

        }); 

Everything works. Bud when i put bunch of them into array

 private TextInputControl[] textControls = {
        profileNameField,....

and using reference

textControls[0].setOnKeyReleased(new CustomEve..

All of the sudden there is no effect. Why is that? I port my application from swing everything worked there bud javafx seems to be little differend in this aspect.This is not that big of a deal i already did it the NON-lazy way.And my fx application works perfectly bud out of curiosity,Why does it behave like this, can anyone explain?

Это было полезно?

Решение

When you define it in this way:

@FXML private TextField profileNameField = new TextField();

the assigned value is never user and will be rewritten by FXMLLoader. So the array is initialized with incorrect values, initialize it after profileNameField is initialized by FXMLLoader, in the initialize method (yep, I think, the English should be edited in this answer :) ):

public class <Controller name> implements javafx.fxml.Initializable { // necessary
    ...
    private TextInputControl[] textControls;
    @FXML private TextField profileNameField;
    ...
    public void initialize(URL url, ResourceBundle resourceBundle) {
        textControls = {
                profileNameField,
                ...
        };
        ...
    }
    ...
}

see http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top