Mario Ortegón
Mario Ortegón

Reputation: 18900

Increasing the font size of a JTextPane that displays HTML text

Lets say that I have a JTextPane that is showing a HTML document.

I want that, on the press of a button, the font size of the document is increased.

Unfortunately this is not as easy as it seems... I found a way to change the font size of the whole document, but that means that all the text is set to the font size that I specify. What I want is that the font size is increased in a proportional scale to what was already in the document.

Do I have to iterate over every element on the document, get the font size, calculate a new size and set it back? How can I do such an operation? What is the best way?

Upvotes: 3

Views: 5315

Answers (3)

Hao Yellow
Hao Yellow

Reputation: 83

After exploring for a long time, I've found a way to zoom the fonts in a JTextPane that displays HTML in and out.

Here's the member function that enables a JTextPane to scale the fonts. It does not handle the images inside the JTextPane.

private void scaleFonts(double realScale) {
    DefaultStyledDocument doc = (DefaultStyledDocument) getDocument();
    Enumeration e1 = doc.getStyleNames();

    while (e1.hasMoreElements()) {
        String styleName = (String) e1.nextElement();
        Style style = doc.getStyle(styleName);
        StyleContext.NamedStyle s = (StyleContext.NamedStyle) style.getResolveParent();
        if (s != null) {
            Integer fs = styles.get(styleName);
            if (fs != null) {
                if (realScale >= 1) {
                    StyleConstants.setFontSize(s, (int) Math.ceil(fs * realScale));
                } else {
                    StyleConstants.setFontSize(s, (int) Math.floor(fs * realScale));
                }
                style.setResolveParent(s);
            }
        }
    }
}

Upvotes: 0

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

In the example that you linked to you will find some clues to what you are trying to do.

The line

StyleConstants.setFontSize(attrs, font.getSize());

changes the font size of the JTextPane and sets it to the size of the font that you pass as a parameter to this method. What you want to to set it to a new size based on the current size.

//first get the current size of the font
int size = StyleConstants.getFontSize(attrs);

//now increase by 2 (or whatever factor you like)
StyleConstants.setFontSize(attrs, size * 2);

This will cause the font of the JTextPane double in size. You could of course increase at a slower rate.

Now you want a button that will call your method.

JButton b1 = new JButton("Increase");
    b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            increaseJTextPaneFont(text);
        }
    });

So you can write a method similar to the one in the example like this:

public static void increaseJTextPaneFont(JTextPane jtp) {
    MutableAttributeSet attrs = jtp.getInputAttributes();
    //first get the current size of the font
    int size = StyleConstants.getFontSize(attrs);

    //now increase by 2 (or whatever factor you like)
    StyleConstants.setFontSize(attrs, size * 2);

    StyledDocument doc = jtp.getStyledDocument();
    doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
}

Upvotes: 4

OscarRyz
OscarRyz

Reputation: 199333

You could probably use css and modify only the styles font.

Since it renders th HTML as it is, changing the css class may be enough.

Upvotes: 1

Related Questions