Styled text in JavaFX?

2019-06-15 02:37发布

问题:

I'm switching my chat client over from Swing to JavaFX, and I have run into a graphical problem. Previously, I was using a JTextPane to insert styled text, but upon switching to FX, I could not find a single component capable of doing what the JTextPane did, and from this answer, I know that FX's TextPane will not do the job either. Sorry if this is obvious to you JavaFX veterans, but I can't quite seem to find anything.

Please, if you could recommend a component like this, it would be much appreciated. Thank you in advance.

回答1:

Styled text will is available in JavaFX 8 (included in jdk8 and jre8) via the TextFlow component.

Here are the details and samples of the feature.

The feature was implemented by the following Multi-line, multi-style, rich text support issue and the related issues it references.

For older (now obsolete) JavaFX versions such as JavaFX 2.2, use a WebView to display styled text and an HTMLEditor to edit it. Or you can place multiple Text or Label nodes in a FlowPane.