Java Swing How to - Wrap long words in JTextPane








Question

We would like to know how to wrap long words in JTextPane.

Answer

import java.awt.BorderLayout;
//w ww .  j  a  v  a  2s  .  co  m
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 200);
    f.getContentPane().setLayout(new BorderLayout());
    JTextPane jtp = new JTextPane();
    jtp.setEditorKit(new WrapEditorKit());
    JScrollPane jsp = new JScrollPane(jtp);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    f.getContentPane().add(jsp, BorderLayout.CENTER);
    jtp.setText("thisIsATestThisisAtestthisIsATestThisisAtestthisIsATestThisisAtest");
    f.setVisible(true);
  }
}

class WrapEditorKit extends StyledEditorKit {
  ViewFactory defaultFactory = new WrapColumnFactory();

  public ViewFactory getViewFactory() {
    return defaultFactory;
  }
}

class WrapColumnFactory implements ViewFactory {
  public View create(Element elem) {
    String kind = elem.getName();
    if (kind != null) {
      if (kind.equals(AbstractDocument.ContentElementName)) {
        return new WrapLabelView(elem);
      } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
        return new ParagraphView(elem);
      } else if (kind.equals(AbstractDocument.SectionElementName)) {
        return new BoxView(elem, View.Y_AXIS);
      } else if (kind.equals(StyleConstants.ComponentElementName)) {
        return new ComponentView(elem);
      } else if (kind.equals(StyleConstants.IconElementName)) {
        return new IconView(elem);
      }
    }
    return new LabelView(elem);
  }
}

class WrapLabelView extends LabelView {
  public WrapLabelView(Element elem) {
    super(elem);
  }

  public float getMinimumSpan(int axis) {
    switch (axis) {
    case View.X_AXIS:
      return 0;
    case View.Y_AXIS:
      return super.getMinimumSpan(axis);
    default:
      throw new IllegalArgumentException("Invalid axis: " + axis);
    }
  }
}