Search code examples
javaswingjtextpane

JTextpane does not respect visibleEditorRect with java 8


Run the below code both java 6 and java 8, and see the result. Line breaks works within the bounds of visibleEditorRect on java 6, but on java 8 the string overflow the bounds. Is there any workaround about this problem.

import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicTextPaneUI;

public class TextPaneBug {

public static void main(String[] args) {
    JFrame f = new JFrame() ;
    JTextPane text = new BugTextPane() ;
    f.add(text);
    text.setText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f.setVisible(true);
}

static class BugTextPane extends JTextPane
{
    public BugTextPane() {       
        setUI(new BasicTextPaneUI(){
            @Override
            protected Rectangle getVisibleEditorRect() {
                Rectangle r = super.getVisibleEditorRect() ;
                Rectangle newr = new Rectangle(r.width / 2 - 300 , r.height/2 - 300 , 600 ,600) ;
                return newr;
            }
            protected void paintSafely(java.awt.Graphics g) {
                super.paintSafely(g);
                Rectangle r = getVisibleEditorRect() ;
                g.drawRect(r.x,r.y,r.width,r.height);
            };
        }

            );
    }
}
}

Solution

  • i resolve the problem with reverting the breaking behavior to java 6 functionality. Overwriting getBreakWeight and getBreakSpot methods by copying from java 6 in an extention of LabelView is enough.