Reputation: 145
I would like to make one particular word in my Label italic but i cant find any solutions, ive looked everywhere and tryed so many different ways.
Label reference = new Label(lastNameText + ", " + firstNameText + ". (" + yearText + "). "
+ titleOfArticleText + ". " + titleOfJournalText + ", "
+ volumeText + ", " + pageNumbersText + ". " + doiText);
background info - i want "titleOfJournalText" to be italic and the rest just plain, they are all strings btw which where once in their own textfields
Upvotes: 1
Views: 4664
Reputation: 159341
The standard Label text can only have a single style for a given Label.
However you can mix text styles easily using a TextFlow. Usually you can just reference the TextFlow directly without placing it in an enclosing Label.
You can still place the TextFlow in a Label if you wish by setting the TextFlow as a graphic of the label. Note that when you do this, the in-built eliding functionality (where the label text is truncated to dots if there is not enough space to display the label) of the Label will not work with the TextFlow.
Here is a small sample program that references Einstein's theory of special relativity.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.*;
import javafx.stage.Stage;
public class StyledLabel extends Application {
public static final Font ITALIC_FONT =
Font.font(
"Serif",
FontPosture.ITALIC,
Font.getDefault().getSize()
);
@Override
public void start(final Stage stage) throws Exception {
Text lastNameText = new Text("Einstein");
Text firstNameText = new Text("Albert");
Text yearText = new Text("1905");
Text titleOfArticleText = new Text("Zur Elektrodynamik bewegter Körper");
Text titleOfJournalText = new Text("Annalen der Physik");
titleOfJournalText.setFont(ITALIC_FONT);
Text volumeText = new Text("17");
Text pageNumbersText = new Text("891-921");
Text doiText = new Text("10.1002/andp.19053221004");
Label reference = new Label(
null,
new TextFlow(
lastNameText, new Text(", "),
firstNameText, new Text(". ("),
yearText, new Text("). "),
titleOfArticleText, new Text(". "),
titleOfJournalText, new Text(", "),
volumeText, new Text(", "),
pageNumbersText, new Text(". "),
doiText
)
);
stage.setScene(new Scene(reference));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
Upvotes: 1