wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes changing cursor color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing cursor color" Watch "changing cursor color" New topic

changing cursor color

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I've created a JTextField in which I change the back and foreground colors. Is it possible to change the color of the text cursor or do I need to make a custom, colored cursor. Sorry if this is pathetically basic, but I can't seem to find the answer.
steve richardson

Joined: Dec 16, 2000
Posts: 3
Most things are possible in Java! Well, at least when it comes to the way swing components appear to the user.
JTextField inherits numerous methods from JTextComponent with which to modify and interrogate your cursor. Actually a Cursor is what your mouse controls.... The text cursor is known as a Caret. You can get a handle to the Caret object itself and manipulate it directly. Simpler though is to use among other things, getCaretColor and setCaretColor methods, e.g.
JTextField txtUserName = new JTextField();
This should assist you I believe.
Kind Regards,
N Kl

Joined: Feb 06, 2004
Posts: 1
Hi, is there an easy way to change the cursor color for ALL the textfields in an application?
Donny Nadolny
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
You could just extend JTextField and add a constructor that accepts a caret color. Also, if you mean change the color of every JTextField multiple times I can only suggest making a method.

- Donny Nadolny<br />The pen is mightier than the sword, and considerably easier to write with.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You could also set it through the look and feel...

Just put this code at the start of your main() (or anywhere before any GUIs are displayed), and it will set all the carets in all the textfields to red (or any other color you specify).

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link: http://aspose.com/file-tools
subject: changing cursor color
It's not a secret anymore!