4

I am writing my first JavaFX application and I cannot find how to make some of the text in a Label to be superscript.

In Swing it was easy enough to use HTML tags, but this option is not available in JavaFX.

I have searched through many of the api's including Label, Font, TextFlow, Oracle docs and samples, and the internet in general.

Thank you.

TonyJ
  • 43
  • 2
  • 7

2 Answers2

4

One approach would be to substitute a WebView and use the loadContent() method of WebEngine.

WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.loadContent(
    "<h1>G<sub>&mu;&nu;</sub>=8πT<sub>&mu;&nu;</sub>; E=mc<sup>2</sup>");

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [*How to display HTML in JavaFX Application*](http://stackoverflow.com/q/30829164/230513). – trashgod Jul 30 '15 at 11:37
4

The WebView is more flexible, but, depending upon which font you are using and the content of the superscript, there are Unicode superscript characters such as U+00B2 for the superscript 2.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
clartaq
  • 5,320
  • 3
  • 39
  • 49
  • Thank you clartaq. I realise now that I could have also used Unicode for JLabel as well. I have been using Unicode to produce Registered Trade Mark and Copyright symbols, however, I was unaware of Unicode superscript. – TonyJ Aug 01 '15 at 03:50