Article View: borland.public.jbuilder.java.swing
Article #8142Superscripts and subscripts
From: Chris Denham
Date: Tue, 17 Oct 2000 00:00
Date: Tue, 17 Oct 2000 00:00
98 lines
3802 bytes
3802 bytes
I am writing an application that requires output of subscripts,
superscripts.
At the moment I am trying to this via creation of a StyledDocument an
assigning it to a JTextPane. I had partial success with the subscripts but I
am completely stumped with superscripts.
The problems with subscripted characters are:
1. They are "simple mindedly" rendered as 2 points smaller than the current
font (which does not make sense for larger font sizes)
2. The characters are not dropped below the baseline of surrounding text.
The problem with superscripted characters they look exactly like subscripts,
i.e. They don't even remotely resemble a superscript.
Does anyone know how to fix/workaround this "feature"?
The documentation suggests that the document is rendered in a JTextPane by
some sort of "pluggable" component. Has anyone got an example of how to
customise the way styled text is rendered?
Thanks
Chris.
BTW.
In case it's any use to anyone, I've included the simple document class I've
been using which lets me do things like:
EasyDocument doc = new EasyDocument();
doc.setFontSize(12);
doc.bold();
doc.add("some bold text\n");
doc.unbold();
doc.add("the chemical formula for water H");
doc.subscript();
doc.add("2");
doc.unsubscript();
doc.add("O");
jTextPane1.setStyledDocument(doc);
/*--------------------------------------------------------------------------
----*/
/* EasyDocument.java */
import java.awt.Component;
import javax.swing.text.*;
public class EasyDocument extends DefaultStyledDocument {
public SimpleAttributeSet attributes = new SimpleAttributeSet();
public void add(String text) {
try {
insertString(getLength(), text, attributes);
}
catch(BadLocationException ex) { }
}
public void add(Component c) {
try {
SimpleAttributeSet componentAttr = new SimpleAttributeSet();
StyleConstants.setComponent(componentAttr, c);
insertString(getLength(), " ", componentAttr);
}
catch(BadLocationException ex) { }
}
public void bold() { StyleConstants.setBold(attributes, true); }
public void unbold() { StyleConstants.setBold(attributes, false); }
public void italic() { StyleConstants.setItalic(attributes, true); }
public void unitalic() { StyleConstants.setItalic(attributes, false); }
public void underline() { StyleConstants.setUnderline(attributes,
true); }
public void ununderline() { StyleConstants.setUnderline(attributes,
false); }
public void subscript() { StyleConstants.setSubscript(attributes,
true); }
public void unsubscript() { StyleConstants.setSubscript(attributes,
false); }
public void superscript() { StyleConstants.setSuperscript(attributes,
true); }
public void unsuperscript() { StyleConstants.setSuperscript(attributes,
false); }
public void setFontSize(int size) { StyleConstants.setFontSize(attributes,
size); }
public int getFontSize() { return
StyleConstants.getFontSize(attributes); }
public void setFontFamily(String family)
StyleConstants.setFontFamily(attributes, family); }
public String getFontFamily() { return
StyleConstants.getFontFamily(attributes); }
}
Technical Details
Message-ID:
<39ec31f6_1@dnews>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,f8f2df5801ca77b0,start X-Google-Attributes: gid111d45,public From: "Chris Denham" <c.m.denham@open.ac.uk> Subject: Superscripts and subscripts Date: 2000/10/17 Message-ID: <39ec31f6_1@dnews> X-Deja-AN: 682422134 X-Trace: 17 Oct 2000 04:03:18 -0800, 137.108.41.217 X-MSMail-Priority: Normal X-Priority: 3 Reply-To: "Chris Denham" <c.m.denham@open.ac.uk> Newsgroups: borland.public.jbuilder.java.swing X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400