wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing cursor color" Watch "changing cursor color" New topic
Author

changing cursor color

Anonymous
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.
thanks!
steve richardson
Greenhorn

Joined: Dec 16, 2000
Posts: 3
Ted,
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();
txtUserName.setCaretColor(Color.red);
This should assist you I believe.
Kind Regards,
Steve
N Kl
Greenhorn

Joined: Feb 06, 2004
Posts: 1
Hi, is there an easy way to change the cursor color for ALL the textfields in an application?
Thanks
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
Bartender

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
 
subject: changing cursor color