Hans
Hans

Reputation: 521

JavaFX Change textcolor of disabled textfield in CSS

I have a texfield in a stage:

@FXML
private TextField tfAdUsername;

tfAdUsername.setPromptText(userName);
tfAdUsername.setDisable(true);

The textcolor is lightgray, i want to change it to black:

.text-field:readonly {
   -fx-background-color: #E0E0E2;
   -fx-border-color: #94BBDA;
   -fx-text-fill: #000000;
}

.text-field:disabled {
   -fx-background-color: #E0E0E2;
   -fx-border-color: #94BBDA;
   -fx-text-fill: #000000;
}

This doesn't change the textcolor. What is the correct CSS Property to use?

Upvotes: 6

Views: 13084

Answers (2)

WonderWorld
WonderWorld

Reputation: 966

For changing the opacity use:

.text-input:disabled {
-fx-opacity: 1.0;
}

in the css file.


To change the textcolor used (after i read the question properly i know this wasn't asked.)

From the modena.css stylesheet:

.root {        
     /* Used for the inside of text boxes, password boxes, lists, trees, and
 * tables.  See also -fx-text-inner-color, which should be used as the
 * -fx-text-fill value for text painted on top of backgrounds colored
 * with -fx-control-inner-background.
 */
-fx-control-inner-background: derive(-fx-base,80%);
/* Version of -fx-control-inner-background for alternative rows */
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%);

/* One of these colors will be chosen based upon a ladder calculation
 * that uses the brightness of a background color.  Instead of using these
 * colors directly as -fx-text-fill values, the sections in this file should
 * use a derived color to match the background in use.  See also:
 *
 * -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color
 * -fx-text-background-color for text on top of -fx-background
 * -fx-text-inner-color for text on top of -fx-control-inner-color
 * -fx-selection-bar-text for text on top of -fx-selection-bar
 */
}
.root {
    -fx-text-inner-color: #FF01F3;
}

For example:

   @Override
    public void start(Stage primaryStage) {
    VBox root = new VBox();
    root.setAlignment(Pos.CENTER);
    Button btn = new Button();
    TextField text= new TextField();
    btn.setText("Press me!");
    btn.setOnAction((ActionEvent event) -> {
        text.setText("Goodbye World");
    });
    root.getChildren().addAll(btn, text);   
    Scene scene = new Scene(root, 300, 250);
    scene.getStylesheets().addAll(getClass().getResource("css.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();
}

And the color of the text in the textfield will be pink?purple?

This is by the way not a means to change the color for each textfield individually, if you change this for the root then all the textfields will use this color instead of the usual black. I don't know how to change the color for one textfield to blue and for another one in the same application to red.

Upvotes: 2

ItachiUchiha
ItachiUchiha

Reputation: 36722

The reason why color turns grey on disabling is because of the opacity change. Just try adding the following css to your textfield.

-fx-opacity: 1.0;

Working example (using setStyle() )

public class KeyStroke extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        TextField textField = new TextField("Itachi");
        textField.setDisable(true);
        textField.setStyle("-fx-opacity: 1.0;");
        root.getChildren().add(textField);
        Scene scene = new Scene(root, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 14

Related Questions