Solved

Need to find position on screen of Caret in text area

Posted on 1998-07-05
6
197 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to start Neon 20 89
simple java question 3 44
HTTPSessionBindingListernter not configured in deployment descriptor 2 40
map related example 6 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now