Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of text in javafx TextField?

Tags:

javafx

I want to change font color in TextField .I found -fx-background-color , -fx-border-color for changing the color of background and border but nothing for text.

like image 532
Saeed Masoumi Avatar asked Jul 11 '14 16:07

Saeed Masoumi


People also ask

How do you change the color of a TextField?

Step 1: Locate the file where you have placed the TextField widget. Step 2: Inside the TextField widget, add the style parameter and assign the TextField widget. Step 3: Inside the TextField widget, add the color parameter and set the color of your choice.

How do you change the color of a label in JavaFX?

ChangeListener<Number> listener = (obser, old, newV) -> text. setFill(getColor()); for (int i = 0; i < slider.

How do I make text bold in JavaFX?

Making Text Bold or Italic To make the text look bold, use the FontWeight constant of the font method as shown in Example 8. t. setFont(Font. font("Verdana", FontWeight.

What is the correct syntax for creating a text in a JavaFX's Project class?

Creating a Text Node Text text = new Text(); The class Text contains a property named text of string type, which represents the text that is to be created. After instantiating the Text class, you need to set value to this property using the setText() method as shown below.


2 Answers

Setting the -fx-text-fill works for me.

See below:

if (passed) {     resultInfo.setText("Passed!");     resultInfo.setStyle("-fx-text-fill: green; -fx-font-size: 16px;"); } else {     resultInfo.setText("Failed!");     resultInfo.setStyle("-fx-text-fill: red; -fx-font-size: 16px;"); } 
like image 69
Martin Pfeffer Avatar answered Sep 30 '22 19:09

Martin Pfeffer


The CSS styles for text input controls such as TextField for JavaFX 8 are defined in the modena.css stylesheet as below. Create a custom CSS stylesheet and modify the colors as you wish. Use the CSS reference guide if you need help understanding the syntax and available attributes and values.

.text-input {     -fx-text-fill: -fx-text-inner-color;     -fx-highlight-fill: derive(-fx-control-inner-background,-20%);     -fx-highlight-text-fill: -fx-text-inner-color;     -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%);     -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),         linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);     -fx-background-insets: 0, 1;     -fx-background-radius: 3, 2;     -fx-cursor: text;     -fx-padding: 0.333333em 0.583em 0.333333em 0.583em; /* 4 7 4 7 */ } .text-input:focused {     -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; } 

Although using an external stylesheet is a preferred way to do the styling, you can style inline, using something like below:

textField.setStyle("-fx-text-inner-color: red;"); 
like image 35
jewelsea Avatar answered Sep 30 '22 19:09

jewelsea