The moose likes Swing / AWT / SWT and the fly likes disable keyboard for Textfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "disable keyboard for Textfield" Watch "disable keyboard for Textfield" New topic

disable keyboard for Textfield

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
I have a JTextField which shows a string value based on some conditions, now I want it to function in such a way that when a textfield gets focus it should not receive any keyboard events, in other words I want to disable any sort of listeners for the textfield. Is this possible? I have tried using removeXYZListener() etc, but still I'm able to use the spacebar/delete & in this way change the text.Pls help.

- Vinod<br />-------<br />SCJP2
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Vinod,
There is two ways you can choose from:
(1) yourTextField.setEditable( false );
Sets the specified boolean to indicate whether or not this TextComponent should be editable. A PropertyChange event ("editable") is fired when the state is changed.
(2) yourTextField.setEnabled( false );
Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.
let me know if this was some help
Yoel Stern

Sun Certified Programmer for JAVA 2 Platform
[This message has been edited by yoel stern (edited August 14, 2001).]
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Thanx a lot Yoel,
I was actually thinkin around the same lines & did it, I wanted to use some of the removeListener methods to do it , or maybe disableEvents() but somehow they didnt work (do you know how to do it?), so then I had to resort to the old way of disabling the fields.
I agree. Here's the link:
subject: disable keyboard for Textfield
It's not a secret anymore!