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

Need to find position on screen of Caret in text area

I need to be able to trap the graphical (x & y) (not the # of characters position) of a keyevent in a standard awt TextComponent.  I don't need a lot of implementation details, just a way to either (ideal, perhaps impossible) get an x and y for the keydown event, or else a way to get the x and y position of a particular character position in a text component.  I need to be able to present an external window just below the place where text is being edited.  Any ideas?
0
drasin
Asked:
drasin
  • 4
  • 2
1 Solution
 
drasinAuthor Commented:
Adjusted points to 150
0
 
fontaineCommented:
I would proceed in two steps:

(1) TextComponent extends Component. Component has a getLocationOnScreen() method
that returns the top-left corner of the component's bounds in the coordinate space of the
screen.

(2) With getCaretPosition() in a TextComponent, we can determine the position of the
text insertion caret. We extract the substring at the left. The width of this string on screen
combined with the location of the component on screen gives what we are searching for.

To know the width/height of a string, we first have to get the font (using the getFont()
method of Component), build a FontMetrics object and use the appropriate methods.

For (2), the code will look like (I don't guarentee that it is 100% correct):

Font font = myTextField.getFont();
FontMetrics metrics = new FontMetrics(font);
String string = myTextField.getText().substring(0, myTextField.getCaretPosition());
int width = metrics.stringWidth(string); // the width of the string in pixels
int height = metrics.getHeight(); // the height of the string in pixels

You will also need to take the component insets into account to have something more precise.

With the old event model, the keyDown() method was receiving an Event object as a parameter. Event.x and Event.y were giving the coordinates of the event. These were the coordinates relative to the component position if I remember well. Step (2) could be avoided like this.

Good luck!
0
 
drasinAuthor Commented:
Sorry-as I said, this has to work with a _standard_ (i.e. not extended) AWT TextComponent.  I'm currently working on something ugly using fontmetrics.  If anyone can solve my problem before I can, you are welcome to the points.  The tool I am developing must be deployed on a number of apps, which already have their own edit areas, which have many differences but all are AWT TextComponents of one sort or another--I cannot change the text component itself, I need to do this with what AWT gives me.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
drasinAuthor Commented:
sorry, fontaine, I guess I didn't read your comment carefully the first time.  What you are suggensting is more or less what I was doing anyway, bu still since you had it right (as best I can tell)I will give you the points anyway.  Please re-post and excuse me.
0
 
fontaineCommented:
I was not understanding your comment, indeed. Actually, this solution does not require
a lot of coding. Maybe that the old event model would give the most direct solution.
If I find a better way, I'll post to you, but I have the feeling we don't have that much options.

-----------------------

I would proceed in two steps:

     (1) TextComponent extends Component. Component has a getLocationOnScreen() method
     that returns the top-left corner of the component's bounds in the coordinate space of the
     screen.

     (2) With getCaretPosition() in a TextComponent, we can determine the position of the
     text insertion caret. We extract the substring at the left. The width of this string on screen
     combined with the location of the component on screen gives what we are searching for.

     To know the width/height of a string, we first have to get the font (using the getFont()
     method of Component), build a FontMetrics object and use the appropriate methods.

     For (2), the code will look like (I don't guarentee that it is 100% correct):

     Font font = myTextField.getFont();
     FontMetrics metrics = new FontMetrics(font);
     String string = myTextField.getText().substring(0, myTextField.getCaretPosition());
     int width = metrics.stringWidth(string); // the width of the string in pixels
     int height = metrics.getHeight(); // the height of the string in pixels

     You will also need to take the component insets into account to have something more precise.

     With the old event model, the keyDown() method was receiving an Event object as a parameter. Event.x
     and Event.y were giving the coordinates of the event. These were the coordinates relative to the
     component position if I remember well. Step (2) could be avoided like this.

     Good luck!
0
 
drasinAuthor Commented:
THIS DOES NOT WORK!!!! I accepted the answer before trying it (my mistake)  A keydown event in the 1.02  model always returns x and y of 0,0; and using fontmetrics + screenposition fails when a text area has been scrolled.  Java may not allow what I am trying to do at all.  Pretty pathetic for a language which claims to be the end-all of computing.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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