3

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);
}
muaz
  • 368
  • 6
  • 10
  • For better help sooner, post an [SSCCE](http://sscce.org/) (of your best attempt). – Andrew Thompson Jan 24 '12 at 04:28
  • BTW - what should happen if a line has an image embedded, and the image height is greater that the viewable height of the view port? – Andrew Thompson Jan 24 '12 at 04:33
  • Thank you for your reply, the viewport is full screen and I am editing customized epub files. However; if there is an image as you described then the only thing is to start the viewport with a line or an image however its long. – muaz Jan 24 '12 at 15:52

1 Answers1

4

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.

StanislavL
  • 55,186
  • 9
  • 58
  • 88
  • Thank you for your help, I am already using modelToView() and scrollRectToVisible() methods, but in my situation the font is resizable, and sometimes apart of line displayed at the bottom of the viewport, so if i scroll with the height of viewport this part of line will appear at the top of the viewport again???. – muaz Jan 24 '12 at 16:11
  • I think yes. The caret rect height = row height so no matter which fonts are used in the row. – StanislavL Jan 25 '12 at 05:50