Link to home
Create AccountLog in
Avatar of darkpegasus5
darkpegasus5

asked on

Finding a JTextPane's "height"

If I'm scrolling a JTextPane, how can I find out the total height (what Y value I should stop at) of all the text? The getLength() property tells me how many characters there are so I could probably figure out how many fit on one line based on the font and then look at height from there.

But there has to be an easier way since the JTextPane can mix in other components like images as well. So how do I figure out what y coordinate in my JViewPort to quit scrolling?
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sciuriware
sciuriware

In general this kind of methods only work if the component is currently displayed.
It will return 0 if the component was not added to a container and/or the container was not
.setVisivle(true);

;JOOP!
Avatar of darkpegasus5

ASKER

It might help if I put my problem into context. Below is the code that I currently have while scrolling the JViewPort. The top part only scrolls up if you haven't reach the "top" of the document.

The problem is, how do I figure out when to NOT scroll down. If there are only 4 lines and I look at the height (of 100) I'll still be able to scroll down 96 units I shouldn't have. If I say don't scroll if the position is less than 100, then when the document size is greater it won't scroll either.

Point p = vp.getViewPosition();
                        if (yOffset == -1) // we're moving up
                        {
                              if (p.y > 0) {
                                    p.translate(0, yOffset);
                                    vp.setViewPosition(p);
                                    System.out.println("point: " + p);
                              }
                        } else {
                              if (p.y > sizeOfDocument? && p.y > textPane.getHegiht())
                              {
                                    p.translate(0, yOffset);
                                    vp.setViewPosition(p);
                              }
                        }
The method I gave answers in pixels.
If you want to control units you must go into the graphic representation size of the objects.
It's far better to count lines.

;JOOP!
I agree, but the height of a document with 4 lines will read as 100 because it becomes the size of the JViewPort. Is there a way to get the number of lines in the document?
Yes, count the newlines in .getText();
Btw.: the size of the viewport depends on the size of the scroll pane.
You could set its size also.

;JOOP!
Here is the code I ended up using (sans the System.out.printlns) but I don't understand why the textPane is one JViewPort height larger?

if (yOffset == -1) // we're moving up
                        {
                              if (p.y > 0)
                              {
                                    p.translate(0, yOffset);
                                    vp.setViewPosition(p);
                                    System.out.println("point: " + p);
                              }
                        }
                        else // scroll down by 1
                        {
                              if (!(textPane.getSize().getHeight() <= vp.getSize().getHeight()))
                              {
                                    System.out.println("yep, we're good to scroll down");
                                    System.out.println("p.y: " + p.y);
                                    if (p.getY() < textPane.getSize().getHeight()-vp.getSize().getHeight())
                                    {
                                          p.translate(0, yOffset);
                                          vp.setViewPosition(p);
                                    }
                              }
                        }