Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+19 votes

I wanted to change the text when typed to white to match the design but couldn't, any idea's on how to do this?

asked in CSC305 Fall 2023 by (1 point)

3 Answers

+8 votes

To change the text color of a typed input in JavaFX, such as in a TextField or TextArea, you need to apply CSS styling to that particular component.
Example CSS

.text-field {

-fx-text-fill: white; /* Changes text color to white */

}

Link the scene to css:
scene.getStylesheets().add("path/to/your/style.css");

OR
you can set it directly in your code
TextField textField = new TextField();
textField.setStyle("-fx-text-fill: white;"); // Set the text color to white

answered by (2.1k points)
+8 votes

You can use JavaFX's event handling mechanisms to achieve that. For example, add an event handler for the OnKeyTyped event, which is triggered when the user types into the TextField. When a key is typed, you change the text color to white by updating the style of the TextField using the setStyle method.

Check this out: https://docs.oracle.com/javafx/2/api/javafx/scene/input/KeyEvent.html

answered by (241 points)
+2 votes

From what I have learned this semester creating a CSS file is the best way to handle most UI design problems, in this case, the CSS file would look like the following,

.text-field
{

-fx-test-fill: white;

}

and if you want to add other things to the design, you could add more to this CSS class.

answered by (1 point)
...