Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextField setEnabled vs setEditable

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.

like image 653
Tariq Avatar asked Feb 11 '14 06:02

Tariq


People also ask

What does setEditable do in Java?

The setEditable() Method You could modify the program so it has a listener, but this would not follow the original design for the program. A better idea (for this program) is to prevent the user from entering text into the wrong text field. This can be done with the setEditable() method: outText.

What is setEnabled in Java?

setEnabled() Enables or disables the given add-on. This function must usually be called in the context of a user action, such as the click handler for a button.

What is the role of JTextField?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt.


1 Answers

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

  1. How to Use Text Fields
  2. Component#setEnabled()
like image 74
ravibagul91 Avatar answered Sep 28 '22 04:09

ravibagul91