3

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);
            };
        }

            );
    }
}
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
mfidan
  • 647
  • 5
  • 19
  • Why it's so important to override visible rect? I guess you use some tricks to achieve some behavior. – StanislavL May 15 '15 at 11:15
  • We have an text editor with paging and the paging logic based on visibleEditorRect. So our editor does not work with java 8 as expected. – mfidan May 15 '15 at 11:18
  • SO as I can see the problem is changed wrap. It could be http://stackoverflow.com/questions/8666727/wrap-long-words-in-jtextpane-java-7 here. – StanislavL May 15 '15 at 12:32
  • Yes it seems about wrapping behavior change at java 7 and there are lots of bug report about new wrapping behavior but no fix release. Oracle does not accept the situation as a bug i think. – mfidan May 15 '15 at 13:20
  • You can see my answer in the post. It fixes the behavior but requires to patch EditorKit – StanislavL May 15 '15 at 13:34

1 Answers1

1

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.

mfidan
  • 647
  • 5
  • 19