How to create a smooth zoomable JTextPane / JTextArea in Swing
01:58 22 Sep 2026

All my previous questions were only from roadblocks I faced on trying my own different approaches so here is the actual problem.

I'm trying to achieve a MSWord-like zoom slider which allows you to zoom to basically any percent you want to. Take for instance this answer, while it does work, it doesn't essentially zoom in 'smoothly' but zooms in based on fixed sizes.

So far I've tried the following approaches:

  • Using a JTextArea I was able to get the indentation working because they took in float values, but since the font size was an int, it looked really bad

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.Scrollable;
    import javax.swing.SwingUtilities;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    
    public class TextEditorWithPane {
    
      public static void main(String[] args) {
          SwingUtilities.invokeLater(() -> {
              new TextEditorWithPane();
          });
      }
    
      public TextEditorWithPane() {
          JFrame jf = new JFrame();
    
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jf.setLayout(new BorderLayout());
    
          ViewPanel view = new ViewPanel();
    
          ContentPanel content = new ContentPanel();
          view.setBackground(Color.BLACK);
          view.setLayout(new VerticalPageLayoutManager(40));
          view.add(content);
    
          JScrollPane scrollPane = new JScrollPane();
          scrollPane.setViewportView(view);
          scrollPane.setBorder(BorderFactory.createEmptyBorder());
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
          jf.add(scrollPane);
    
          jf.setSize(1100, 600);
          jf.setLocationRelativeTo(null);
          jf.setVisible(true);
    
          System.out.println(content.getWidth());
      }
    
      private class ContentPanel extends JTextPane {
          private static final long serialVersionUID = 1L;
    
          static final float FIXED_WIDTH = 904f;
    
          public ContentPanel() {
              StyledDocument document = getStyledDocument();
              MutableAttributeSet attrs = getInputAttributes();
    
              addComponentListener(new ComponentAdapter() {
                  @Override
                  public void componentResized(ComponentEvent e) {
                      float scale = getWidth() / FIXED_WIDTH;
    
                      StyleConstants.setLeftIndent(attrs, scale * 40);
                      StyleConstants.setRightIndent(attrs, scale * 40);
                      StyleConstants.setSpaceAbove(attrs, scale * 40);
                      StyleConstants.setFontSize(attrs, (int) (scale * 15f)); // This is the issue
    
                      document.setParagraphAttributes(0, document.getLength() + 1, attrs, false);
                  }
              });
          }
      }
    
      private static class ViewPanel extends JPanel implements Scrollable {
          private static final long serialVersionUID = 1L;
    
          @Override
          public boolean getScrollableTracksViewportWidth() {
              return true;
          }
    
          @Override
          public boolean getScrollableTracksViewportHeight() {
              return false;
          }
    
          // Other Scrollable methods ...
      }
    }
    
  • Then I later tried using JTextArea and deriveFont(float) together but that didn't work either, font scaling was inconsistent and using deriveFont(AffineTransform) also had unnatural behavior.

  • Finally I thought maybe I could just control the painting of the JTextArea/JTextPane but that question brought no success.

    Is there any better way to obtain smooth scaling in Java Swing?

java swing jtextarea jtextpane