• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

obtaining mouse pointer position within a document

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm trying to find out how to obtain mouse pointer position within a document and then translate it to line and character number. This functionality exists in many applications, i.e. JBuilder or TextPad.
I've been searching Java API for a solution, but I haven't found one yet.
Any ideas?
Thanks!
Kamilla
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that by mouse position, you mean the little vertical bar left when you click on a text componenent with the mouse, which is called the caret. It's location is called the dot.
Assuming you are working with a subclass of JTextComponent, try
int offset= textComponent.getCaret().getDot();
That gives you the character offset from the beginning of the document to the dot.
Then, if you are working with a JTextArea, you can use
int line = textArea.getLineOfOffset(offset);
If not using JTextArea, but PlainDocument or another Document organized the same way, you might try
int line = component.getDocument().getDefaultRootElement().getElementIndex(offset);
As for StyledDocuments, I don't know, and don't have my books here.
 
kamilla miesak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
JTextComponent was one of the classes I looked at, but I had no idea that the term "caret" was what I should have been looking for.
Thanks again!
 
kamilla miesak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question:
is there a way to get an offset within a specific line as oppose to an offset for an entire JTextArea. When I do:
int offset= JTextArea.getCaret().getDot();
int line = JTextArea.getLineOfOffset(offset);
I do get correct line numbers, but the offset of the last character from a previous line is being carried over to the next line and calculated continuosly for the entire document.
I've tried couple of ways to calculate it separately, but nothing worked so far.
Thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic