Solved

Need to find position on screen of Caret in text area

Posted on 1998-07-05
6
202 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
Industry Leaders: 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!

 

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 150 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pagenation logic how it is working in my code 1 71
JAVA API design with micro service cloud in mind 1 82
Html Table Looping (part 2) 5 35
web project error add remove 1 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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