JEditorPane & text linewrap
843806Nov 14 2007 — edited Nov 15 2007Hello,
I have a problem forcing a JEditorPane to line wrap not at a word boundary.
The JEdtiroPane is set inside a JScrollPane but because I need to be able to cope with very long stream of characters with no space between them (real life demand) I have to disable horizontal scrolling (vertical scrolling is based on need). If horizontal scrolling is not disabled the result is a very long line with very long horizontal scroll (which is what I want to avoid).
My document type needs to be html (although the bellow example does not show this, it is a demand based on my real needs) & the component cannot be a JTextArea (again � based on my real needs).
I tried forcing my JEditorPane to wrap the text by manipulating its preferred. I was hoping that line wrap occur when the preferred width is greater then the available (viewport) width.
But I had no success whatsoever...
The bellow code segment shows my problem. As you can see without the scrolling the lines are simply cut so that I am missing text segments unless there is a space (like between the a�s and b�s).
Any ideas?
public class TestScroll extends JFrame {
JPanel mainPanel;
JScrollPane mainScroller;
/** Creates a new instance of ScrollTest */
public TestScroll() {
this.mainPanel = new JPanel(new BorderLayout());
mainScroller = new JScrollPane(new TextBlock());
mainScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
mainScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mainPanel.add(mainScroller);
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
this.setPreferredSize(new Dimension(200,200));
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TestScroll st = new TestScroll();
}
class TextBlock extends JEditorPane {
public TextBlock() {
setContentType("text/html");
setEditorKit(new HTMLEditorKit());
setText("<html> <head> </head> <body>" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
"ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +
"dddddddddddddddddddddddddddddddddddddddddddd" +
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
"ffffffffffffffffffffffffffffffffffffff" +
"ggggggggggggggggggggggggggggggggggggggggggggggggggggggg" +
"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" +
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" +
"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" +
"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" +
"lllllllllllllllllllllllllllllllllllllllllllllll" +
"oooooooooooooooooooooooooooooooooooooooooo" +
"pppppppppppppppppppppppppppppppppppppppppp" +
"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" +
"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" +
"ssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
"ttttttttttttttttttttttttttttttttttttttttttttttttttttttx" +
"uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" +
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" +
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" +
" END </body></html>");
setEditable(false);
}
}