为什么这两个组件具有相同的行为,如果我实例化它们2次不同?

时间:2017-04-29 22:35:01

标签: java javafx fxml controlsfx

以下是声明:

@FXML
private CustomTextField txtFieldUsuario;

@FXML
private CustomPasswordField txtFieldPassword;

这是初始化:

@Override
public void initialize(URL location, ResourceBundle resources) {
    new TextFieldUtil(txtFieldUsuario).clearable();

    new PasswordFieldUtil(txtFieldPassword).clearable();

    adminLogin();

    imageView.setImage(new Image("/foto/logo_fix.png"));

    Icono.agregarImagenBoton(btnIniciarSesion, Icono.INICIAR_SESION);

    enterKeyPressed();
}

这是TextFieldUtil类:

public class TextFieldUtil extends CustomTextField{
    private static final Logger LOGGER = LogManager.getLogger();

    private CustomTextField[] textFields;

    public TextFieldUtil(CustomTextField... textFields) {
        this.textFields = textFields;
    }

    public void propiedades() {

    }

    public void clearable() {
        for (CustomTextField customTextField : textFields) {
            customTextField.setRight(Icono.BORRAR_TEXTFIELDS);

            customTextField.getRight().autosize();

            customTextField.getRight().setCursor(Cursor.HAND);

            customTextField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (customTextField.getText().isEmpty()) {

                    customTextField.getRight().setVisible(false);

                } else {

                    customTextField.getRight().setVisible(true);

                    customTextField.getRight().addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
                        if (event.isPrimaryButtonDown() && !customTextField.getText().isEmpty() && customTextField.getRight().isVisible()) {
                            customTextField.setText("");
                        }
                    });
                }
            });
        }
    }

    public void mascara(TextFieldMascara textFieldMascara) {
        switch (textFieldMascara) {
            case TELEFONO:
                break;
            case RNC:
                break;
            case PRECIO:
                break;
        }
    }
}

这是PasswordUtil类:

public class PasswordFieldUtil extends CustomPasswordField{
    private static final Logger LOGGER = LogManager.getLogger();

    private CustomPasswordField[] passwordFields;

    public PasswordFieldUtil(CustomPasswordField... passwordFields) {
        this.passwordFields = passwordFields;
    }

    public void propiedades() {

    }

    public void clearable() {
        for (CustomPasswordField customPasswordField : passwordFields) {
            customPasswordField.setRight(Icono.BORRAR_TEXTFIELDS);

            customPasswordField.getRight().autosize();

            customPasswordField.getRight().setCursor(Cursor.HAND);

            customPasswordField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (customPasswordField.getText().isEmpty()) {

                    customPasswordField.getRight().setVisible(false);

                } else {

                    customPasswordField.getRight().setVisible(true);

                    customPasswordField.getRight().addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
                        if (event.isPrimaryButtonDown() && !customPasswordField.getText().isEmpty() && customPasswordField.getRight().isVisible()) {

                            customPasswordField.setText("");

                        }
                    });
                }
            });
        }
    }
}

这是带有结果的图像,仅包含图像的第二个组件。当我在第二个组件中单击它时,被擦除的组件就是第一个组件(CustomTextField)。

Here is the image

如果有人能告诉我为什么会这样,我会提前感激。

1 个答案:

答案 0 :(得分:0)

您在此代码中的场景图中设置了相同的Node实例。因此,MouseEvent.MOUSE_PRESSED的事件过滤器设置为相同的图标。

customTextField.setRight(Icono.BORRAR_TEXTFIELDS);

customPasswordField.setRight(Icono.BORRAR_TEXTFIELDS);

尝试更改代码,例如:

customTextField.setRight(new ImageView( yourImage ));

customPasswordField.setRight(new ImageView( yourImage ));