• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

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?
0
darkpegasus5
Asked:
darkpegasus5
  • 4
  • 3
1 Solution
 
sciuriwareCommented:
JTextPane jt;
............................
int height = jt.getHeight();  // Inherited from JComponent.

;JOOP!
0
 
sciuriwareCommented:
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!
0
 
darkpegasus5Author Commented:
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);
                              }
                        }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sciuriwareCommented:
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!
0
 
darkpegasus5Author Commented:
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?
0
 
sciuriwareCommented:
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!
0
 
darkpegasus5Author Commented:
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);
                                    }
                              }
                        }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now