PDA

View Full Version : [FIXED] Beta4 CheckBox change event not working



andrey
3 Jun 2008, 10:09 AM
The CheckBox change event is not calling back the listener with Beta4:


import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.layout.FillLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;

public class TestCheckBox implements EntryPoint {
public void onModuleLoad() {
Viewport viewport = new Viewport();
viewport.setLayout(new FillLayout());

CheckBox cb = new CheckBox();

viewport.add(cb);
cb.addListener(Events.Change, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
GWT.log("CheckBox change", null); // Not being called in Beta4
Info.display("Debug", "CheckBox change");
}
});
viewport.layout();
RootPanel.get("slot1").add(viewport);
}
}
Run the above code and check/un-check the CheckBox and notice no Info or GWT log.

Thx,
Andre

flow
3 Jun 2008, 12:19 PM
Hi Andre,

have you tried the Event.ONCLICK instead of the Event.Change?
Cheers,
Flo~

Grandiosa
4 Jun 2008, 1:48 AM
Yep, the OnClick will make it work, but that contradicts the Javadoc for CheckBox.

Here is what I have found (see attached example program)

1. A CheckBox's listener is not triggered on Change events as specified in its Javadoc. However, OnClick events work.

2. Radio listeners can be triggered using either Change and OnClick events.

3. Using OnClick events the Radio's listener is triggered when the radio is clicked, regardless of its previous state.

4. Using Change events, the Radio's listener is triggered both when a Radio is deselected and selected.




public class Main implements EntryPoint {

public void onModuleLoad() {

Viewport vp = new Viewport();
vp.setLayout(new FitLayout());

ContentPanel panel = new ContentPanel(new CenterLayout());

FormPanel form = new FormPanel();
form.setFieldWidth(210);
form.setWidth(380);

CheckBox cb1 = new CheckBox();
cb1.setFieldLabel("Doesn't fire");
cb1.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent fe) {
Info.display("Info", "Checkbox1 triggered");
}
});

form.add(cb1);

CheckBox cb2 = new CheckBox();
cb2.setFieldLabel("Fires ok");
cb2.addListener(Events.OnClick, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent fe) {
Info.display("Info", "Checkbox2 triggered");
}
});
form.add(cb2);


final Radio radio1 = new Radio();
radio1.setName("radio1");
radio1.setFieldLabel("First");
radio1.setValue(true);
radio1.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio1", "Change event");
}
});
radio1.addListener(Events.OnClick, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio1", "OnClick event");
}
});

final Radio radio2 = new Radio();
radio2.setName("radio2");
radio2.setFieldLabel("Second");
radio2.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio2", "Change event");
}
});

RadioGroup radioGroup = new RadioGroup("Whatever");
radioGroup.setFieldLabel("Order");
radioGroup.add(radio1);
radioGroup.add(radio2);
form.add(radioGroup);
panel.add(form);

vp.add(panel);
vp.layout();
RootPanel.get().add(vp);
}
}

darrellmeyer
4 Jun 2008, 11:31 AM
Fix is in SVN. Please validate.

Grandiosa
6 Jun 2008, 2:05 AM
Darrel,

Now the CheckBoxes work ok, but the Radio listeners are "messed" up.
Using the example program below shows that:

- Radio1 listener is called on program startup, which is ok I guess even if the listener was actually added after the Radio is set programmatically using setValue() in the program.

- Clicking Radio2 fires ChangeEvent listener of Radio1. Clicking Radio3 fires listener of Radio2, ...

- Using OnClick events however works ok for both Radios and CheckBoxes.

I tried debugging your code but got lost somwhere between method calls :">

But the first line in Field.fireChangeEvent(Object oldValue, Object newValue) looks wrong to me:

if (oldValue != value) {

I guess should use equals instead?


example program:



public void onModuleLoad() {

Viewport vp = new Viewport();
vp.setLayout(new FitLayout());

ContentPanel panel = new ContentPanel(new CenterLayout());

FormPanel form = new FormPanel();
form.setFieldWidth(210);
form.setWidth(380);

// checkbox using Change event listener
CheckBox cb1 = new CheckBox();
cb1.setFieldLabel("Box1");
cb1.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent fe) {
Info.display("Info", "Checkbox1 triggered");
}
});

form.add(cb1);

// using OnClick listener also works
CheckBox cb2 = new CheckBox();
cb2.setFieldLabel("Box2");
cb2.addListener(Events.OnClick, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent fe) {
Info.display("Info", "Checkbox2 triggered");
}
});
form.add(cb2);


final Radio radio1 = new Radio();
radio1.setName("radio1");
radio1.setFieldLabel("First");
radio1.setValue(true);
radio1.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio1", "Change event");
}
});

final Radio radio2 = new Radio();
radio2.setName("radio2");
radio2.setFieldLabel("Second");
radio2.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio2", "Change event");
}
});

final Radio radio3 = new Radio();
radio3.setName("radio3");
radio3.setFieldLabel("Third");
radio3.addListener(Events.Change, new Listener<FieldEvent>() {
public void handleEvent(FieldEvent be) {
Info.display("Radio3", "Change event");
}
});


RadioGroup radioGroup = new RadioGroup("Whatever");
radioGroup.setFieldLabel("Order");
radioGroup.add(radio1);
radioGroup.add(radio2);
radioGroup.add(radio3);
form.add(radioGroup);
panel.add(form);

vp.add(panel);
vp.layout();
RootPanel.get().add(vp);
}