Duško Mirković
Duško Mirković

Reputation: 321

Set text color of JavaFX TextField without CSS

I've been working a lot in JavaFX lately, by writing code only -- no CSS no FXML and I want to keep it that way. I've managed to do all of the stuff I wanted that way except that now I can't set the text color of TextField without using CSS, so I'm asking for advice here. Is there any way to do it, no matter how hacky it is?

Note that I'm still using Java 8 version.

Upvotes: 0

Views: 862

Answers (2)

brat
brat

Reputation: 584

I could not make it work. But made another solution. Although the caret color also changes.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

public class TextField_TextColor_Demo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        TextField txt = new TextField();
        CustomTextFieldSkin skin = new CustomTextFieldSkin(txt);
        txt.setSkin(skin);
        skin.setHighlight(Color.RED);
        HBox box = new HBox(txt);;
        Scene scene = new Scene(box, 300, 100);
        stage.setScene(scene);
        stage.show();
    }
    
    public class CustomTextFieldSkin extends TextFieldSkin{
        public CustomTextFieldSkin(TextField textField) {
            super(textField);
        }
        public void setHighlight(Paint value) {
            setTextFill(value);         
        }
    }
    
     public static void main(String... args){
            Application.launch(args);
     }
 
}

Seems it is possible to color the caret separately, according these two answers:

https://stackoverflow.com/a/47876289/7989121

https://stackoverflow.com/a/27323035/7989121

Both use caretPath though, which I could not use due it not being visible according to my IDE

Upvotes: 0

Sai Dandem
Sai Dandem

Reputation: 9869

Considering your points : "No CSS" and "how hacky it can be.." below is one way to achieve the text coloring.

import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TextField_TextColor_Demo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField textField = new TextField();
        textField.setSkin(new TextFieldSkin(textField){
            @Override
            protected void layoutChildren(double x, double y, double w, double h) {
                super.layoutChildren(x, y, w, h);
                if(textField.getProperties().get("colorChanged")==null) {
                    textFill.setValue(Color.RED);
                    textField.getProperties().put("colorChanged",true);
                }
            }
        });
        StackPane root = new StackPane(textField);
        Scene scene = new Scene(root, 300,300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String... args){
        Application.launch(args);
    }
}

Upvotes: 1

Related Questions