Search code examples
javafontsformatsuperscript

Simple text formatting, such as superscript, in Java?


Possible Duplicate:
How do I write superscript word for checkbox text in java?

So I have this very simple program that basically consists of a GUI with a few text fields and a button.

The idea is that the user enters numbers into three fields and presses the button. Then what happens is a few mathematical procedures are carried out in the background, and the resuling answer is presented in a fourth text field.

Now, this does the job, but the formatting looks awful. At the very least, I would like to have part of the output superscripted. I have next to no experience with these things, but thought I would be able to get the hang of this on my own, but I'm stuck. I think I need to use AttributedString and possibly Font, but I can't get anything to work. And I've found no tutorials.

Does anyone have any quick pointers? That'd be most helpful!


Solution

  • Do you need the output field to be editable? If not, try using a JLabel with HTML code. Something like:

    jLabel4.setText("<html>ax<sup>2</sup>+bx+c</html>");
    

    You can add a border to that JLabel to make it look like a text field.