Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Showing a Popup menu under the caret of a Text Field

843807Aug 30 2010 — edited Aug 30 2010
Hi all,

I'm trying to implement a kind of an intelliscence or autocomplete for when the user is typing inside a jTextField.
To do this I want to create a popup menu which will show directly beneath the caret of the text field - this pop up menu will display the different options for the user to chose from to complete what he's typing.

I searched for solutions for this kind of problem and found this:
[http://forums.sun.com/thread.jspa?threadID=5443811]

The suggested solution in this link looks very close to what I need, with only one problem -

when using this line:
Rectangle r = textArea.modelToView( textArea.getCaretPosition() );
the X and Y positions that I get into r, are relative to the textfield itself and not for the position of the caret on screen as I need it, and because of this when I show the popup menu at these coordinates, it doesn't show where I want it - directly beneath the caret.

I guess my bottom line question is: How can I get the position of the caret on screen?

Thanks a lot to anyone who can help :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 27 2010
Added on Aug 30 2010
6 comments
232 views