Editable property of text field in Java in detail
Answers
Answered by
4
Stack Overflow
sign up log in
Questions Jobs Tags Users Badges Ask
Read this post in our app!
up vote8down votefavorite
JTextField setEnabled vs setEditable
java swing jtextfield
What is the difference between JTextField.setEnabled() and JTextField.setEditable()? In my code I have an Instance of a class inherited from JTextField. But when I set its property setEnabled(false) I can still edit and process its contents in my program. However when I set its property setEditable(false) I can no longer edit its text. If it is so. Then what is the purpose of setEnabled()property here?
My Code for inherited class is:
private static class CCField extends JTextField{ private static final long serialVersionUID = 1L; public CCField() { this( DEFAULT_COLUMN_COUNT ); } public CCField(final int cols) { super( cols ); }
Added INFO When I call the setEnabled() property of this class it does not show any effect on the text field and it still remains enabled. I have a container Jcomponent in my code which have this CCField as a child component. So when I try to disable it using setEnabled(false) it still editable. Whereas when I try to disable it using setEditable(false) then it is disabled. This is how I am accessing this textField in my container:
JComponent jComp = DDEUtil.getComponent(icTableLayout,icDS); ((JTextField)jComp.getComponent(1)).setEditable(false);
And what is going on in DDEUtil.getComponent is too complex as it involve a number of classes and not possible to post here.
I wonder I have not overridden any method of this component so why is it showing this behavior.
share improve this question
askedFeb 11 '14 at 6:57

Tariq
891●6●18●44
editedFeb 11 '14 at 7:17
8
Post a complete program demonstrating the problem. setEnabled(false) disables a text field completely. setEditable(false) still allows selecting and copying its content, but doesn't allow modifying its value. – JB Nizet Feb 11 '14 at 7:00
@JBNizet I think that qualifies to be an answer... – Jim Garrison Feb 11 '14 at 7:05
1
@JimGarrison: I think that the actual question is: why setEnabled(false) doesn't disable my text field. But the OP needs to post more code to have an answer to this question. – JB Nizet Feb 11 '14 at 7:10
When I call the setEnabled() property of this class it does not show any effect on the text field and it still remains enabled. Something wrong in the code you are not showing ;-) So either post a SSCCE or solve it without help, nobody can guess which invisible line is wrong ... – kleopatra Feb 11 '14 at 10:28
I have show every thing except DDEUtil.getComponent which is too large, complex and generic, as it is generating a lot of components using reflection. – Tariq Feb 11 '14 at 10:55
add a comment
2 Answers
order by
active oldest votes
up vote17down voteaccepted
In my case setEditable(false) is graying the field and setEnabled(false)not graying the field.
TextField are editable by default. The code setEditable(false) makes the TextField uneditable. It is still selectable and the user can copy data from it, but the user cannot change the TextField's contents directly.
The code setEnabled(false), disables this TextField. It is not selectable and the user can not copy data from it and the user cannot change the TextField's contents directly.
Useful Links
How to Use Text FieldsComponent#setEnabled()
share improve this answer
answeredFeb 11 '14 at 8:36

Jugadu
1,623●1●7●19
Very strange this is, IMHO the "official" is that disabled are totally grayed out. Editable simply lock the field but "only" change its background grayed while the text keeps readable. Can you confirm and probably improve your answer? Tested with Java 1.8. – Thomas Nov 23 '16 at 15:16
add a comment

up vote2down vote
While setEnabled(false) grays out the field comletely, setEditable(false) just prevents it from beeing edited, but it will still look the same.
share improve this answer
answeredFeb 11 '14 at 7:37

das_j
1,547●2●18●40
not in my case. Here setEditable(false) is graying the field whereas setEnabled(false) is doing nothing... – Tariq Feb 11 '14 at 7:48
I just wrote a sample, found here: pastebin.com/myMFiYaJ – das_j Feb 11 '14 at 7:54
It works perfect, there must be something wrong in the code of CCField – das_j Feb 11 '14 at 7:55
Though it does not solve the problem but still I got the answer of my actual question i.e. Different b/w setEnabled and setEditable – Tariq Feb 11 '14 at 8:04
Could you post the entire code of CCField? It would help a lot– das_j Feb 11 '14 at 8:22
show 1 more comment
Your Answer
Body
Add picture
Log in
OR
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2017 Stack Exchange, Inc
Similar questions
English,
7 months ago
English,
7 months ago
Math,
7 months ago
Math,
1 year ago
CBSE BOARD X,
1 year ago