?
Solved

Need to find position on screen of Caret in text area

Posted on 1998-07-05
6
Medium Priority
?
205 Views
Last Modified: 2010-03-30
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
Comment
Question by:drasin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 

Author Comment

by:drasin
ID: 1224677
Adjusted points to 150
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1224678
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
 

Author Comment

by:drasin
ID: 1224679
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:drasin
ID: 1224680
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
 
LVL 5

Accepted Solution

by:
fontaine earned 600 total points
ID: 1224681
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
 

Author Comment

by:drasin
ID: 1224682
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question