0

I'm trying to bold some text in a setter, but when it's displayed, it's not working. I'll just jump into some code so it makes sense...

private static class Spell {
    private final String name;
    private final String school;
    private final String display_class;
    //etc.

.

Text t = new Text("TEST: ");
        t.setStyle("-fx-font-weight: bold;");

        this.name = name + "\n\n";
        this.school = school + "\n";
        this.display_class = t + display_class + " " + spell_level + "\n";
//etc.

Displaying the list:

if (!newValue.isHeader()) {
            tooltip.setText(newValue.getName() + newValue.getSchool() + newValue.getDisplay_Class() //etc.

The text is displayed within a ScrollPane as a Text object: Text tooltip = new Text();

The list displays values like Level: 0 Range: 25ft etc. With this setup, is it at all possible to make just the "Level:" and "Range:" parts bold? Currently, what is displayed when it prints t is Text[text="TEST: ",x=0.0,y=0.0, alignment=LEFT, origin=BASELINE, boundsType=LOGICAL...etc. etc, yet when adding t to a pane and displaying it that way, it shows up correctly bolded. I don't know what to do at this point.

hego64
  • 345
  • 1
  • 5
  • 17
  • 1
    The Only way I can think of is like this : https://stackoverflow.com/a/34230669/4167500 In addition if you don't like the css you could set the text Font like : setFont(Font.font("Aria", FontWeight.BOLD, 20)); – JKostikiadis Oct 05 '17 at 22:29
  • TextFlow is displaying the same extra junk as before, and still without the bold. As far as setting font, wouldn't that just set it for the entire list instead of being able to specify one word? Edit: I also have to be somewhere in a few minutes, so I'll get back to you and anyone else who sees this when I return. Thanks in advance! – hego64 Oct 05 '17 at 22:39
  • 3
    You will have to break your Text into smaller one, for example into two parts, the first one would be the bold and the second the regular. After that when you specify their font ( or just bold style ) you need to display them as one entity, of course you can achieve that by using any layout like FlowPane, HBox etc but TextFlow is the best for such kind of job. As you will see in the link's example the style is been set to the each Text part and not to the entire TextFlow. This is what it will be displayed : http://prntscr.com/gtt3xt – JKostikiadis Oct 05 '17 at 22:44
  • Either all the `Tooltip`'s text has to be bold or none. I am guessing you can create a custom `Tooltip` and override this behavior. – SedJ601 Oct 06 '17 at 01:13
  • 1
    @JKostikiadis This actually fixed it for me. Originally when I had to leave I spent like 2 min on it, and that got me nowhere. Now, after having time to play with it some more, that worked. I set the TextFlow inside of a ScrollPane, and separated the text into into 2 Text objects, one bold one not, and it finally looks how I wanted it to. Thank you so much for this. So annoying how something so seemingly simple took so long to do... – hego64 Oct 06 '17 at 01:28

1 Answers1

1
Text t = new Text("TEST: ");
...
this.display_class = t + display_class + " " + spell_level + "\n";

In the line above, you are concatenating the output of toString() of your Text object instance (t) with other strings, that where your extra junk comes from, and it is not probably what you wanted to do.

Unfortunately Text won't support multistyle text strings.

guido
  • 18,864
  • 6
  • 70
  • 95
  • Note: [`t.getText()`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/Text.html#getText--) gets the actual current value string of the text node rather than the `toString()` on `t` that you are currently implicitly used, so you probably want to use `t.getText()`. – jewelsea Oct 05 '17 at 23:20
  • There is no point as Text wouldn't support text fragments with different styles anyway. – guido Oct 05 '17 at 23:31
  • Yes, it won't by itself solve his original problem, but may solve some auxiliary issue he is noting of "displaying the same extra junk as before". Perhaps what he is calling "extra junk" is the toString on the text object, I don't know, but that is what I would guess. – jewelsea Oct 05 '17 at 23:46
  • So, is there no way to actually do this, even if I have to rework everything I have right now? And even if I don't concatenate them, and try just the bold part and change the type itself to Text, it still displays the 'junk', the toString stuff, and not what I would expect. – hego64 Oct 06 '17 at 00:20
  • 1
    You can rework your code by making instances of a few `Text`s (each with its own style), and then use this class for instance https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/TextFlow.html to layout them – guido Oct 06 '17 at 08:57