Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2007

Tip: Looking for answers? Try searching our database.

JTextField - how to disable selecting text in this component?

Thread view: 
dako - 28 Dec 2006 10:28 GMT
Hi!

Has anybody an idea, how to do it? I'll be lucky.

Regards, dako
mearvk - 28 Dec 2006 10:37 GMT
> Hi!
>
> Has anybody an idea, how to do it? I'll be lucky.
>
> Regards, dako

Have you tried JTextField.setEditable(false) ?

If this isn't the problem could you try and make the problem a bit
clearer?

Mearvk
dako - 28 Dec 2006 11:16 GMT
mearvk napisal(a):
> > Hi!
> >
[quoted text clipped - 8 lines]
>
> Mearvk

OK, I'll try explain the problem more clearly :)  even though I have't
felt comfortable writing EN yet...

I don't need to disable selecting the component for a while or ages...,
but only text within the component.

I hope, it's an enough discribe my problem.
Brandon McCombs - 28 Dec 2006 22:37 GMT
> mearvk napisal(a):
>>> Hi!
[quoted text clipped - 16 lines]
>
> I hope, it's an enough discribe my problem.

setEnabled(false) and setEditable(false) are different and
setEditable(false) would be what you want to prevent people from putting
text in it. Disabling the component is done by setEnabled() which wasn't
what Mearvk proposed to you.
dako - 29 Dec 2006 07:21 GMT
Brandon McCombs napisal(a):

> setEnabled(false) and setEditable(false) are different and
> setEditable(false) would be what you want to prevent people from putting
> text in it. Disabling the component is done by setEnabled() which wasn't
> what Mearvk proposed to you.

Sorry. I forgotten say I don't want to prevent component from puting
text in it but only whilst selecting text in it. Is it imposible?
dako - 29 Dec 2006 07:21 GMT
Brandon McCombs napisal(a):

> setEnabled(false) and setEditable(false) are different and
> setEditable(false) would be what you want to prevent people from putting
> text in it. Disabling the component is done by setEnabled() which wasn't
> what Mearvk proposed to you.

Sorry. I forget say I don't want to prevent component from puting text
in it but only whilst selecting text in it. Is it imposible?
mearvk - 29 Dec 2006 12:53 GMT
> Brandon McCombs napisal(a):
> >
[quoted text clipped - 5 lines]
> Sorry. I forget say I don't want to prevent component from puting text
> in it but only whilst selecting text in it. Is it imposible?

Try subclassing JTextField. When the mouseDrag event fires inside the
JTextField do something like return immediately or set the focus on the
parent JComponent. You will want to catch keyboard input also. (Block
things like ctrl+A, ctrl+C, shift+end and shift+home)

I think it can be done but it will require some hacking and time. There
may be a better way but this is what I can figure.

Good luck,

Mearvk
Andrew Thompson - 29 Dec 2006 13:34 GMT
...
> > ...I forget say I don't want to prevent component from puting text
> > in it but only whilst selecting text in it. Is it imposible?
....
> I think it can be done but it will require some hacking and time. There
> may be a better way ...

How about - an entirely different /strategy/ in
following 'the path of least surprise' in the use
of GUI components?

It is nonsensical to allow editing of a text field, but
disallow selecting the text (I would normally select
the text simply to delete the content, but if this
scheme were implemented - i'd need to hit 'del, del,
del, del, ..oh heck I'll just use the software of their
direct competitor, instead..').

Andrew T.
dako - 04 Jan 2007 10:04 GMT
Andrew Thompson napisal(a):
> It is nonsensical to allow editing of a text field, but
> disallow selecting the text....

Sorry, but I don't agree with you. It is't nonsensical. You should
remember that necessity is the mother of invention. Apparently somebody
needed the solution as well, because exist special method:

jTextField_userStreet.setHighlighter(null);

Regards, dako.
dako - 04 Jan 2007 09:36 GMT
mearvk napisal(a):

> Try subclassing JTextField. When the mouseDrag event fires inside the
> JTextField do something like return immediately or set the focus on the
[quoted text clipped - 3 lines]
> I think it can be done but it will require some hacking and time. There
> may be a better way but this is what I can figure.

The solution is easier than you think ;) Look at John's post.

Regards, dako.
Real Gagnon - 29 Dec 2006 20:37 GMT
> Sorry. I forget say I don't want to prevent component from puting text
> in it but only whilst selecting text in it. Is it imposible?

Try to use a JLabel instead.

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

dako - 04 Jan 2007 09:15 GMT
Real Gagnon napisal(a):

> Try to use a JLabel instead.

I've already known the solution thanks to John. I might try
JTextField.setHighlighter(null) so the idea is redundant.

Regards, dako
John - 03 Jan 2007 16:39 GMT
> Hi!
>
> Has anybody an idea, how to do it? I'll be lucky.
>
> Regards, dako

You might try JTextField.setHighlighter(null);

John
dako - 04 Jan 2007 09:06 GMT
Hi John!

Thx a lot! That is the thing in comparision with rest of comments :)
The highlighter sounded familiar but I did't think about the null :|

Regards, dako
Jim Sculley - 09 Jan 2007 14:30 GMT
> Hi John!
>
> Thx a lot! That is the thing in comparision with rest of comments :)
> The highlighter sounded familiar but I did't think about the null :|
>
> Regards, dako

All this does is prevent the display of the highlighting color.  The caret still moves, and text is still selected and
can still be cut and pasted.  I doubt this is the behavior you want.

The key question is why do you want to allow typing but not selection?  Is it to prevent cut/copy/paste? If so, remove
the cut copy and paste actions from the action map of the JTextField.

It is not always enough to say what you want.  Sometimes you have to say why you want it in order from the proper
solution to be offered.

Jim S.

Signature

Remove my extraneous mandibular appendages to reply via email.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.