Search code examples
javatextrenderingjtextfield

Power characters in Java


I'm having some problems with text rendering on a JTextField. I would like to print in this field a mathematic expression, with the exponents of powers and the indexes of roots should go to the top of the line, like every power you've ever seen. Besides, I would like the root symbol to extend over the following terms. How can I do that?


Solution

  • Well with a JLabel you can use an HTML superscript but JTextField does not do HTML. So if you don't need to take input, use a JLabel. It is as simple as "<html><sup>123</sup></html>".

    You can still display some characters as superscript in a JTextField through some special UTF characters, the locations of which are:

    Some of what you are asking (like about extending the root symbol) seems a little too much to ask. A JTextField can't do that. I am sure there would be TeX/LaTeX/etc typesetting libraries for Java though if you want real mathematical notation.