JavaFX: Red border for text field

后端 未结 4 2016
生来不讨喜
生来不讨喜 2020-12-28 17:52

I have a form with text fields and I want to give them a red border if I click on \"save\" but e.g. nothing was input in required fields, letters for the \"birthday\" field,

4条回答
  •  生来不讨喜
    2020-12-28 18:31

    When using the javafx8 moderna style, you can use this css to make the border similar to the 'on focus' blue border:

    .text-input.error {
        -fx-focus-color: #d35244;
        -fx-faint-focus-color: #d3524422;
    
        -fx-highlight-fill: -fx-accent;
        -fx-highlight-text-fill: white;
        -fx-background-color:
            -fx-focus-color,
            -fx-control-inner-background,
            -fx-faint-focus-color,
            linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
        -fx-background-insets: -0.2, 1, -1.4, 3;
        -fx-background-radius: 3, 2, 4, 0;
        -fx-prompt-text-fill: transparent;
    }
    

    add the css as classpath resource and load it using:

    scene.getStylesheets().add(
        getClass().getClassLoader().getResource().toString());
    

    then apply it to text fields using:

    // add error class (red border)
    textField.getStyleClass().add("error");
    // remove error class (red border)
    textField.getStyleClass().remove("error");
    

提交回复
热议问题