Solved

Need to find position on screen of Caret in text area

Posted on 1998-07-05
6
193 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
  • 4
  • 2
6 Comments
 

Author Comment

by:drasin
Comment Utility
Adjusted points to 150
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:drasin
Comment Utility
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 150 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now