I have a Button and I'm using javaFX, which I need: button text is written Search, I need 'B' to be in red and 'uscar' in default color, I've tried everything and I'm not getting do this.
I have a Button and I'm using javaFX, which I need: button text is written Search, I need 'B' to be in red and 'uscar' in default color, I've tried everything and I'm not getting do this.
You can create the button with additional text inside:
<Button fx:id="button" graphicTextGap="0.0" mnemonicParsing="false" text="uscar">
<graphic>
<Text fill="RED" strokeMiterLimit="0.0" strokeType="INSIDE" strokeWidth="0.0" text="B" />
</graphic>
</Button>
That way the first character will be in color set to fill
, and the rest will have the button style.
On the button you must leave the value of graphicTextGap
zeroed so that the first letter is not separated from the rest of the text
You will only achieve this result by doing this manually, you can use the textFill
method to achieve the result. See the example below:
public class ColoredButtonTextDemo extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
HBox coloredTextBox = HBoxBuilder.create().spacing(0).children(
LabelBuilder.create().text("B").textFill(Color.RED).build(),
LabelBuilder.create().text("uscar")textFill(Color.BLACK).build();
btn.setGraphic(coloredTextBox);
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}