Question

I am dealing with JEditorPane to display HTML documents and I created a button which scroll me to next view port every time I click it, "as if I am turning a page of a book".

However, sometimes I see a part of a line at the top of the view port, so is there any way to force JScrollBar to scroll to ahead of a line?

I tried setBlockIncrement() member method but it didn't work at all.

Here you are my best attempt:

//get the visible rectangle as well as the most bottom right point. 
Rectangle rec = jEditorPane1.getVisibleRect();
Point pt = new Point((int)rec.getX() +(int)rec.getWidth(),(int)rec.getY() +      (int)rec.getHeight());
// get the offset of the most bottom right point 
int off = jEditorPane1.viewToModel(pt);
try {
//get next viewable rectangle and scroll to it 
rec = jEditorPane1.modelToView(off+100);
rec.height = jEditorPane1.getVisibleRect().height;
jEditorPane1.scrollRectToVisible(rec);
} catch (BadLocationException ex) {
Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
}
Was it helpful?

Solution

Guess you have a position in the Document you want to show. You can get visible rect of the position using modelToView() method. and use the y position to set your viewport. E.g. use scrollRectToVisible passing the y and viewport height in the rectangle parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top