Reputation: 804
Any one can help me to Change the Case in JavaFX project I'm trying to find solution by browse, But i can't Please find the below code I'm trying but it can't get the result
.text {
-fx-font-family: "Arial";
-fx-font-size: 24;
-fx-text-fill: #263248;
-fx-padding: 10 20 10 20;
-fx-cursor: hand;
-fx-text-transform: uppercase; /* this property is not working */
}
Any help would really be appreciated! Thanks!
Upvotes: 4
Views: 6320
Reputation: 131
You can add ChangeListener to Text:
Text text;
//...
text.textProperty().add(
(observer, oldValue, newValue) -> text.setText(newValue.toUpperCase())
);
Upvotes: 0
Reputation: 167
This is a universal way.
You can mark what you need as upper
and lower
in styleClass
and call this method:
public static void textTransform(@NotNull Node root) {
_textTransform(root, ".upper");
_textTransform(root, ".lower");
}
private static void _textTransform(@NotNull Node root, @NotNull String selector) {
boolean upper = selector.equalsIgnoreCase(".upper");
for (Node node : root.lookupAll(selector)) {
if (node instanceof Text) {
Text text = (Text) node;
text.setText(upper ? text.getText().toUpperCase() : text.getText().toLowerCase());
} else
if (node instanceof Label) {
Label label = (Label) node;
label.setText(upper ? label.getText().toUpperCase() : label.getText().toLowerCase());
} else
if (node instanceof Button) {
Button button = (Button) node;
button.setText(upper ? button.getText().toUpperCase() : button.getText().toLowerCase());
}
}
}
Upvotes: 1
Reputation: 668
Unfortunately, there's no CSS property that handles text transformation in JavaFX (currently none, see the JavaFX CSS Reference). But you can actually do it in Java with just a single line of code!
For an instance, you have a Label
node which has an ID named label
.
<Label fx:id="label" text="Hello!" />
In this case, you can reference the node
by its ID and set its text toUpperCase()
with a controller class.
// Reference to the node you wish
// to transform texts to uppercase.
@FXML private Label label;
@Override
// Assuming you have Initializable class implemented.
public void initialize(URL arg0, ResourceBundle arg1) {
// Get the current text value of the node.
String text = label.getText();
// Then update the text into whatever case you like.
label.setText(text.toUpperCase());
}
OR
Simply straight from a Java code (one straight line of code).
Label label = new Label("Hello!".toUpperCase());
Upvotes: 1