Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to hide and show cursor in JTextField?

5,032 views
Skip to first unread message

Roy

unread,
Jan 3, 2008, 8:05:36 PM1/3/08
to
Hi,

Is there any way to hide the cursor in the JTextField and show the
cursor when I click this field? It'd be great if I click other area
or components in the panel, hide the cursor in the JTextField again.

Thanks a lot.
Roy

Mark Space

unread,
Jan 3, 2008, 8:57:11 PM1/3/08
to

Which cursor? Mouse? Insert caret?

Roy

unread,
Jan 3, 2008, 11:13:09 PM1/3/08
to

It is the insert caret.

Andrew Thompson

unread,
Jan 3, 2008, 11:35:53 PM1/3/08
to
Roy wrote:
..

>It is the insert caret.

Your comments do not make much sense to me.

There is an applet here that will pop a dialog with a
JTextField and JTextArea (click the 'Input' button).
<http://www.physci.org/test/resize/>

The 'insert caret' (as I understand it) will only show in
the text field/area that currently has focus, in either the
Metal or System (Win) PLAFs.

To hide the insert caret even when it is in a particular
text widget, just set the widget uneditable or disabled
(see dropdown on main applet GUI).

--
Andrew Thompson
http://www.physci.org/

Message posted via http://www.javakb.com

Roy

unread,
Jan 4, 2008, 12:12:15 AM1/4/08
to

Thanks a lot. I happened to setFocusable(false) for all other
components. So the JTextField is the only component that always has
focus. That is why the insert caret is always blinking.

Anway, I was told by a friend that a simple way to hide the caret in
this situation is to do the following:

MyTextField.getCaret().setVisible(false);

Thanks again.
Roy

Mark Space

unread,
Jan 4, 2008, 2:27:52 PM1/4/08
to
Roy wrote:

> MyTextField.getCaret().setVisible(false);

I was going to suggest the same thing Andrew did. I didn't know about
this method, good find, thanks!

stephens...@gmail.com

unread,
Aug 22, 2012, 8:07:05 AM8/22/12
to
Excellent, thanks!

karthi...@gmail.com

unread,
Apr 22, 2015, 8:29:37 AM4/22/15
to
Other option might be

JTextArea.setCaretColor(getBackground());

Eric Sosman

unread,
Apr 22, 2015, 9:03:13 AM4/22/15
to
"Let's Do The Time Warp Again!"

(It must be admitted, though, that Karthi's answer to a seven-year-
old question is not in the same league as someone else's recent answer
to an *eighteen-year-old* question.)

--
eso...@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

javadi...@gmail.com

unread,
Jun 10, 2015, 6:20:01 AM6/10/15
to
visit for more java interview questions and programs - http://javadiscover.blogspot.com

--------------------------------------
--------------------------------
0 new messages