Tkingovr Tkingovr - 5 months ago 39
Java Question

Toggle TextField required/not required when checkbox checked

I have a text field which I want to toggle as a required field or not based on if a checkbox is checked. I can't get the ValueChangeEventListener to play nice.

This is what I have so far:

private TextField myTextField = new TextField("Name");

private CheckBox myCheckBox;
myCheckBox = createCheckBox();

private CheckBox createCheckbox() {
CheckBox checkBox = new CheckBox("My checkbox");
checkBox.addValueChangeListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
return checkBox;

private void toggleRequired() {
if (myCheckBox.getValue() != true) {
} else {

The valueChangeListener gets fired way too many times going from true to false back to true. Not sure what I'm doing wrong here and would really appreciate some help.

Thank you


Ok I figured it out! It was a very easy solution all I had to change was

 checkBox.addValueChangeListener(new CheckBox.ValueChangeListener() {});

I was referencing some the following properties class instead: