• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField scrolling question?  RSS feed

 
Andrew L. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is my first post here... anyway, I've got a bit of a problem.

Here it is
| - represents cursor/caret
[ ] - represents the JTextField and bold text is the text within it that is visible to the user.
<>Text in JTextField, but is not visible to the user.

< [Hello there m|y name is Andy ] I'm 100 years old>

What I want to be able to do is scroll through like this.
<H [ello there my| name is Andy I] 'm 100 years old>

so the caret stays in the middle and it also shifts the ScrollOffset in such a way that the caret is almost exactly in the middle.

What I'm ultimately trying to achieve is make a couple of characters right after the caret visible to the user.

Edit: A few more questions if no one has the answer to the one above... These might help me quite a lot.
1. Is there a way to find the position of the caret within jTextField in pixels from the left or right?
2. Is there a way to find pixels per character displayed in the JTextBox?
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like:

import java.awt.*;


Add it to the text field using:

 
Andrew L. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't think that would work, because getCaretPosition doesn't return its position in pixels relative to the JTextComponent's contined text. It instead returns its caret position by characters to its left i.e. if you have "112|" (Where | is the caret) It would return 3.

I do find that idea very good though, If only there was a way to find out the pixels each character in the JTextComponent occupies, then I could calculate it by that instead of trying to estimate how many pixels each increment in caret position is.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because getCaretPosition doesn't return its position in pixels relative to the JTextComponent's contined text.


Which is why you also need to use the modelToView() method.

Anyway, I think this approach is closer to what you want. I don't notice any flickering of the caret position, only scrolling of the text:



If only there was a way to find out the pixels each character


For future use you can use the FontMetrics class.
 
Andrew L. Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, Thank you very much !!!
All solved !!!

Guess I'm not that good in Java yet.

Thank you very much matey.

I just read up about modelToView method, think I know what it does now.

Thanks again for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!