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!

Recommended Answers

All 8 Replies

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.

commented: dis_agree with down vote +11

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?

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) { }

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

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

commented: his post was incredibly helpful +3

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.