• Post Reply Bookmark Topic Watch Topic
  • New Topic

JEditorPane with CSS font size attributes  RSS feed

 
Michael Finney
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taken from Chris Smith's excellent post in comp.lang.java.gui (I want to know the answer too.)
The following code attempts to allow the user to enter text, set its font
size to vey large, and then save it. It appears to work, except the HTML
it writes looks like this:
<font size="xx-large">Some Text</font>
when I would expect it to look like either this:
<font size="7">Some Text</font>
or this:
<span style="font-size: xx-large">Some Text</span>
If I change the addCSSAttribute call to read as follows:
doc.getStyleSheet().addCSSAttribute(attr, CSS.Attribute.FONT_SIZE, "7");
then it writes the HTML that I want (the first form), but the current
view of that HTML uses a 7-point font rather than the HTML font size
index of 7 (and that's a huge difference). If I close and reopen the
document, then it shows correctly using a large font for the text.
It appears that I can get either correct behavior of the view *or* write
out correct HTML from the EditorKit write method, but not both.
Unfortunately, both are business requirements, and I'm hoping I won't
have to start inserting Font elements and setting their attributes by
hand.
Incidentally, the problem is being reproduced here using the 1.4 release.
I haven't had the chance to test on earlier JDK versions.
Thanks a bunch!
Chris Smith
Code:
=====================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.io.*;
public class EditorTest extends JFrame
{
private JEditorPane pane;
public EditorTest()
{
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
save();
}
});
JButton bigButton = new JButton("Make It Big!");
bigButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
makeItBig();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(saveButton);
buttonPanel.add(bigButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pane = new JEditorPane();
pane.setEditorKit(new HTMLEditorKit());
getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);
}
private void save()
{
EditorKit kit = pane.getEditorKit();
File f = new File("page.html");
try
{
FileWriter out = null;
try
{
out = new FileWriter(f);
kit.write(out, pane.getDocument(),
0, pane.getDocument().getLength());
}
finally
{
if (out != null) out.close();
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
private void makeItBig()
{
HTMLDocument doc = (HTMLDocument) pane.getDocument();
MutableAttributeSet attr = new SimpleAttributeSet();
doc.getStyleSheet().addCSSAttribute(
attr, CSS.Attribute.FONT_SIZE, "xx-large");
doc.setCharacterAttributes(
0, pane.getDocument().getLength(), attr, false);
}
public static void main(String[] args)
{
new EditorTest().show();
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!