Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Problem with viewToModel()

Thread view: 
Sascha  Konrad - 27 Jun 2005 22:02 GMT
Hello,

I have a JTextPane where I want to determine the position of the mouse.
So if there is a mouse moved event, I will do

int hoverPosition = jCurrentSentencePane.viewToModel(e.getPoint());

to determine the position (as the position of the letter) in the text
pane over which the mouse hovers. Now I have the problem  that this
method returns bogus values if the mouse is somewhere in the text pane,
but not on the text. E.g., when I slowly approch the text somewhere
from the top, it tells me 0 as the hoverPosition, from the bottom it
tells me 35 (the length of my text). I was expecting to receive -1 or
so when I am not over the text, not the beginning or end of my text.
Does anybody have any suggestions?

Thanks.
Christian Kaufhold - 27 Jun 2005 22:19 GMT
> I have a JTextPane where I want to determine the position of the mouse.
> So if there is a mouse moved event, I will do
[quoted text clipped - 5 lines]
> method returns bogus values if the mouse is somewhere in the text pane,
> but not on the text. E.g., when I slowly approch the text somewhere

It is (where?) expected to make a closest guess. This may not always
be implemented in an optimal way.

> from the top, it tells me 0 as the hoverPosition, from the bottom it
> tells me 35 (the length of my text). I was expecting to receive -1 or
> so when I am not over the text, not the beginning or end of my text.
> Does anybody have any suggestions?

Use modelToView() on the result and check whether the point is near there.
Actually, use getUI().viewToModel() to also get aware of the bias.

Christian
Sascha - 28 Jun 2005 00:54 GMT
Hi Christian,

Thanks for your replay.

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
the rectangle of the displated text in the JCurrentTextPane. If that is
possible, then I can just ignore the mouse being on the margin. But I
have not found out how to do that. I can call the JTextArea to get the
size of the margin, but I don't know how to obtain the heigth and width
of the text. (I.e., the right and lower boundary of the displayed
text). Any ideas?

Thanks
Christian Kaufhold - 28 Jun 2005 19:15 GMT

> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.