Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing cursor color

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is there an easy way to change the cursor color for ALL the textfields in an application?
Thanks
 
Donny Nadolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic