wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Subscript & Superscript Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Subscript & Superscript Question " Watch "Subscript & Superscript Question " New topic

Subscript & Superscript Question

Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Is there a way in Java so that we can display superscript and subscript characters like in a chemical formula Ex. CO2,H2O the character 2 should be displayed as a subscript.
I want to display formula as JLabel in my application.In some formulae I may have to display square of a number.....so I need to know how to display characters in superscript also.

Any suggestions are welcome.
[ January 18, 2002: Message edited by: Pomchi Bedi ]
s srikanth

Joined: Jul 06, 2001
Posts: 14
Hi bedi ,
you can use TFormula class for displaying chemical formulae.
The corresponding API says ------>
TFormula provides a simple mechanism for displaying Chemical Formulae.
By default this puts all numbers as subscripts, thus formula can be entered in the form "CH3CH2OH".
However the autoscripting option can be turned off, thus formulae should be input in the following form
"CH3CH2OH" . (actually here 3 and 2 are subscripts..but i m not able to write them as subscripts :-))
If u have any doubts,just check the API Documentation.
[ January 20, 2002: Message edited by: s srikanth ]
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Srikanth,
Thanks for your reply.I could not find TFormula in the jdk1.2.2 api documentation....can you please tell me the name of the Package it belongs to.Also could you please tell me the version of the api I should look in.
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Anybody!! Any Suggestions??
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Yes, one of my personal pet peeves is someone mentioning a class out of a non-standard API and not giving a reference to the package that provides it. However, when not finding the class in the standard APIs, my next source of data is an Internet search engine (like google) and searching for the name of the class, or sometimes the name of the class and "Java" if it is a pretty common name. I found the site ChemSymphony by searching for "TFormula Java", and it claims to provide "a components tool kit for chemistry applications" of the same name... looking at the docs on the site it has a TFormula class that looks like the one Srikanth is talking about... however, it also looks like you have to pay to license ChemSymphony (though they do provide a Lite version for download... don't know if TFormula comes in that...)

If you can't use TFormula from ChemSymphony, it would be pretty simple to make a component to display formulas by overriding the paintComponent() method and adding a StringTokenizer to figure out what size to draw different parts of the String.

[ January 25, 2002: Message edited by: Nathan Pruett ]

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link: http://aspose.com/file-tools
subject: Subscript & Superscript Question
Similar Threads
Displaying Fraction with Java
Using Subscript in Title Bar?
Super and sub string in java to string
Subscripts / Superscripts
representing exponents like x to the power 2 3 and so on