Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set a TextField's value while in ReadOnly mode?

Tags:

java

vaadin

Vaadin widgets offer setEnabled and setReadOnly methods. I want the visual black color readability of the read-only state rather than greyed-out disabled state. My goal is to prevent the user from making direct data entry.

Unfortunately, the read-only mode on a TextField not only prevents the user from data entry. It also prevents me from programmatically setting the value with a call to setValue.

The workaround is to have my code (1) Call setReadOnly with False, (2) Call setValue, (3) call setReadOnly with True.

➤ Is there a simpler way to change the value of a TextField while in read-only mode?

like image 620
Basil Bourque Avatar asked Aug 07 '14 21:08

Basil Bourque


1 Answers

I don't think there is a simpler way to change value of a TextField in read-only mode, if you go through the source code for AbstractField setValue method, you can discover that.

But you still can do that on your own using simple function like:

 public void setValue(TextField textField, String value) {
          if (textField.isReadOnly()) {
              textField.setReadOnly(false);
              textField.setValue(value);
              textField.setReadOnly(true);
          } else {
              textField.setValue(value);
          }
    } 
like image 85
Elkfrawy Avatar answered Nov 03 '22 05:11

Elkfrawy