CSS javafx Color Text

Asked

Viewed 868 times

1

I have a Button and I am using javafx, what I need: the text of the button is written Search, I need the 'B' to be in red and the "uscar" to the default color, I have tried everything and I am not able to do this.

2 answers

1

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 the color set in fill, and the rest will have the button style.

On the button you need to leave the value of graphicTextGap zeroed so that the first letter is not separated from the rest of the text

Botão com a primeira letra com cor diferente

  • forgive my ignorance, but as I add an additional text inside?

  • The text "search" would be on the button, inside the button you put: <Graphic> <Text Fill="RED" strokeMiterLimit="0.0" strokeType="INSIDE" strokeWidth="0.0" text="B" /> </Graphic>, this part will leave the "B" inside the button with the red color

  • I did not find a way to do only with css, I could only do inside fxml

  • so I added this command, only it is giving error in Scene Builder, says it has code that could not read

  • Do it like this: create a button in the Scene Builder, then drag a text inside the button, in the text you put the "B", in the text "uscar", then in properties of the button you put the value of Graphic Text Gap as 0

  • But wouldn’t that be a trick? i managed to do with CSS however depending on the size of the word I need to be readjusting, I wanted to understand this code so that inside netbeans I could make the reasoning to standardize, so there is no way to standardize, because the formatting will be in the view

Show 1 more comment

0

You will only achieve this result by doing it manually, you can use the method textFill 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);
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.