We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,627 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

change blinker/cursor in a JTextArea

hey... I wat to change the... well, the cursor (the little blinking thing in a text area where subsequent typing appears) in a JTextArea. Not the pointer (the mouse icon showing where on the screen the mouse is cuz some ppl thought that was wat I meant) but the cursor in a JTextArea. Not sure if that last part changes the answer, but just in case, there it is!

3
Contributors
8
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

setCaretPosition

public void setCaretPosition(int position)
Sets the position of the text insertion caret for the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.
Parameters:
position - the position
Throws:
IllegalArgumentException - if the value supplied for position is less than zero or greater than the component's text length

from http://docs.oracle.com/javase/1.4.2/docs/api/index.html

edit : link is not pointing to the specific doc, look for JTextComponent, or look for JTextArea and check the class tree.

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

thank you for the prompt response, but unfortunately, that is not quite what I trying to do. I am not trying to change the position of the cursor, I am trying to change the appearance...

For instance, in the terminal, the cursor can be changed to a blinking, solid box, a blinking underline, a blinked checkered box, (and probably more that I can't think of right now)

How do I change appearance of the cursor in a JTextArea?

FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

You might create a custom caret to customize the appearance of a caret. To create a custom caret, write a class that implements the Caret interface — perhaps by extending the DefaultCaret class. Then provide an instance of your class as an argument to the setCaret method on a text component.

from http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

hmm, I know how to implement the Caret interface in java code...

ok, I got all the methods to override:

for void addChangeListener(ChangeListener l) I think I can do 'this.addChangeListener(l);'

and for void setVisible(boolean v) I think I can say 'this.setVisible(v);'

but what about for any of the rest?

public void deinstall(JTextComponent c) {}
public int getBlinkRate() { return 0; }
public Point getMagicCaretPosition() { return new Point(0, 0); }
public int getMark() { return 0; }
public void install(JTextComponent c) {}
public void moveDot(int dot) { }
public void paint(Graphics g) { }
public void removeChangeListener(ChangeListener l) {}
public void setBlinkRate(int rate) {}
public void setMagicCaretPosition(Point p) { }
public void setSelectionVisible(boolean v) { }

FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

ok I realize that was an unfairly long question, and I change it to can you (or anyone else) explain to me what the install + deinstall methods for the Caret interface do and how they work? Oh, and how to write the two ChangeListener methods... I wrote all the other methods, but those 4 methods keep giving me errors and runtime errors...

FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

ok I realize that was an unfairly long question, and I change it to can you (or anyone else) explain to me what the install + deinstall methods for the Caret interface do and how they work? Oh, and how to write the two ChangeListener methods... I wrote all the other methods, but those 4 methods keep giving me errors and runtime errors...

maybe this will shed some light:http://www.java-tips.org/java-se-tips/javax.swing/how-to-implement-a-fancy-caret-for-your-applic.html

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

Yup, thanks a lot, it is much clearer now..

I copied the class and fixed it up a bit.. so now it works fine... Anyway, I am trying to make the Caret a solid box that blinks, like in the terminal.. It starts draws initially correctly, but then after I type anything, it grows to ≈1.5 width, and a little sliver of the caret from the right side is not cleared and left there when the caret blinks..

I could def be wrong (as this is my first experience with Carets) but it seems like I altered the 'paint' method correctly, but then when the rectangular caret is cleared for a blink (as it is supposed to be) part of it stays on the screen and the rest is cleared and redrawn. As a solution, do I have to somehow rewrite the repaint method in the FancyCaret class? (I say somehow because I tried and it said that the original 'repaint()' method is final)

If writing a new redraw() is not the solution, or the whole solution, then what is?

FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

damn, I thought I saw an answer here the other day.. damn it.

FALL3N
Junior Poster in Training
87 posts since May 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3571 seconds using 2.77MB