0

Here is the declaration:

@FXML
private CustomTextField txtFieldUsuario;

@FXML
private CustomPasswordField txtFieldPassword;

And here is the initialization:

@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();
}

Here is the TextFieldUtil class:

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

Here is the PasswordUtil class:

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

                        }
                    });
                }
            });
        }
    }
}

Here is the image with the result, containing the image only the second component. And when I click it in the second the component, the component that gets erased its the first one (CustomTextField).

Here is the image

If someone could tell me why this is happening I would appreciate it in advance.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
guhex
  • 3
  • 1
  • 2
  • `new TextFieldUtil(txtFieldUsuario).clearable();` - where do you assign this anywhere? – EJoshuaS - Stand with Ukraine Apr 29 '17 at 23:23
  • I'm assigning it on the same variable, that's why its void. I have one for combobox and other for spinner, they both work well and without problem. The problem is with these 2. @EJoshuaS – guhex Apr 29 '17 at 23:35

1 Answers1

0

You are setting same Node instance in a scene graph in this code. Because of that, event filters for MouseEvent.MOUSE_PRESSED is set to same icon.

customTextField.setRight(Icono.BORRAR_TEXTFIELDS);

customPasswordField.setRight(Icono.BORRAR_TEXTFIELDS);

Try to change the code for example:

customTextField.setRight(new ImageView( yourImage ));

customPasswordField.setRight(new ImageView( yourImage ));
monolith52
  • 886
  • 2
  • 6
  • 9
  • You are a genius! Thanks man. I appreciate it. I didn't know it was gonna behave like that. Thank you man. Thank you! – guhex Apr 30 '17 at 05:41