> Thanks for your replay.
Please quote some context.
> I found out that when I call jCurrentSentencePane.setMargin(0,0,0,0),
> everything works as expected. Now, I was wondering if I can find out
[quoted text clipped - 4 lines]
> of the text. (I.e., the right and lower boundary of the displayed
> text). Any ideas?
Untested!:
JTextComponent t = ...;
Insets n = t.getInsets();
Rectangle r = new Rectangle(n.left, n.top, t.getWidth() - n.left - n.right, t.getHeight() - n.top - n.bottom);
View v = t.getUI().getRootView(t);
Shape textShape = v.modelToView(v.getStartOffset(), Position.Bias.Forward, v.getEndOffset(), Position.Bias.Backward, r);
Christian
Sascha - 28 Jun 2005 21:12 GMT
Christian, thanks for your efforts, but your solution still does not
work, you still get the width and heigth of the whole text pane.
But you pointed me in the right direction and I think I got it now. You
need to look at each row of each paragrah individually (i only have one
paragrah), determining the width and heigth of this row. Then you
create a new rectangle based on the width of the current row and the
heigth of this row and all previous rows.
Here is the code, in case anybody is interested. It works fine for me
and sets the boolean variable hit to true if the mouse is positioned
over text:
boolean hit=false;
Insets n = jCurrentSentencePane.getInsets();
Rectangle r=new Rectangle(n.left,n.top,0,0);
View sectionView =
jCurrentSentencePane.getUI().getRootView(jCurrentSentencePane).getView(0);
int paragraphCount = sectionView.getViewCount();
for (int paragraphIndex = 0; paragraphIndex < paragraphCount;
paragraphIndex++) {
View paragraphView = sectionView.getView(paragraphIndex);
if (paragraphView instanceof ParagraphView) {
ParagraphView newparagraphView = (ParagraphView) paragraphView;
int rowCount=newparagraphView.getViewCount();
//oldHeigth contains the height of the previous row and is used to
update the y position
int oldHeight=0;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
View rowView= newparagraphView.getView(rowIndex);
r.width=(int) rowView.getPreferredSpan(View.X_AXIS);
r.y=r.y+oldHeight;
r.height=(int) (rowView.getPreferredSpan(View.Y_AXIS));
if(r.contains(e.getX(),e.getY()))
{
//The point was in the rectangle, i.e. mouse is over text
hit=true;
break;
}
oldHeight=(int) rowView.getPreferredSpan(View.Y_AXIS);
}
}
}
It's mighty complicated though, I would be happy to see a simpler
solution.
-- Sascha
Christian Kaufhold - 28 Jun 2005 22:20 GMT
> Christian, thanks for your efforts, but your solution still does not
> work, you still get the width and heigth of the whole text pane.
Again, please quote some context.
AFAICS, the reason my code does not work is a broken (?) optimization in
CompositeView.
Use getEndOffset() - 1 instead of getEndOffset(), then it is not used,
and it does not matter.
Christian
Sascha - 29 Jun 2005 01:06 GMT
Wow, thanks Christian, it works now, you're the Swing master. I guess I
haven't really understoof this whole concept of views and models.
Thanks again for your help.
-- Sascha