Displaying Subscripts and Superscripts

I'm making a program that works with chemical equations, but I don't know how to display subscripted and superscripted text. Does anybody know how?

Comments

  • : I'm making a program that works with chemical equations, but I don't know how to display subscripted and superscripted text. Does anybody know how?
    :

    Not the only way, but perhaps the easiest - use a JEditorPane. If you use HTML, remember that support is only for version 3 - spec is available on http://www.w3.org
    [code]
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;

    public class ChemFormulaDisplay extends JFrame {

    private JEditorPane editorPane =
    new JEditorPane(
    "text/html",
    " CH4
    x2 ");

    public ChemFormulaDisplay() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 300);

    setContentPane(new JScrollPane(editorPane));
    editorPane.setEditable(false);
    }

    public static void main(String[] args) {
    new ChemFormulaDisplay().show();
    }
    }
    [/code]

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • : : I'm making a program that works with chemical equations, but I don't know how to display subscripted and superscripted text. Does anybody know how?
    : :
    :
    : Not the only way, but perhaps the easiest - use a JEditorPane. If you use HTML, remember that support is only for version 3 - spec is available on http://www.w3.org
    : [code]
    : import javax.swing.JEditorPane;
    : import javax.swing.JFrame;
    : import javax.swing.JScrollPane;
    :
    : public class ChemFormulaDisplay extends JFrame {
    :
    : private JEditorPane editorPane =
    : new JEditorPane(
    : "text/html",
    : " CH4 x2 ");
    :
    : public ChemFormulaDisplay() {
    : setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : setSize(300, 300);
    :
    : setContentPane(new JScrollPane(editorPane));
    : editorPane.setEditable(false);
    : }
    :
    : public static void main(String[] args) {
    : new ChemFormulaDisplay().show();
    : }
    : }
    : [/code]
    :
    : ---------------------------------
    : [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

    Wow, thank you so much; that works perfectly. And now I don't have to confuse myself to death with GlyphVectors and MutableAttributeSets, because that was getting me nowhere.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories