PDA

View Full Version : ClassCastException for SimpleComboBox into EditableGrid



listirer
2 Feb 2012, 7:59 AM
Hi, I have a ClassCastException when try to recreate EditableGrid example (http://www.sencha.com/examples/#editablegrid).




....

grid = new EditorGrid<TestDetail>(store, this.createColumnModel());
grid.setBorders(true);
grid.setAutoExpandColumn("id");
grid.getSelectionModel().setSelectionMode(Style.SelectionMode.SINGLE);
mainPanel.add(grid);

....

public ColumnModel createColumnModel() {
List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
...
ColumnConfig idColumn = new ColumnConfig();
idColumn.setId("id");
idColumn.setHeader("ID");
idColumn.setFixed(true);
configs.add(idColumn);

final SimpleComboBox<EnumTest> permissionComboBox = new SimpleComboBox<EnumTest>();
permissionComboBox.setForceSelection(true);
permissionComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
permissionComboBox.add(EnumTest.getAll()); // getAll() return a List<EnumTest>

CellEditor comboEditor = new CellEditor(permissionComboBox);

ColumnConfig permissionColumn = new ColumnConfig();
permissionColumn.setId("permission");
permissionColumn.setHeader("Permission");
permissionColumn.setWidth(150);
permissionColumn.setEditor(comboEditor);
configs.add(permissionColumn);

return new ColumnModel(configs);
}


I view a combo as a label and when select this, the combo compare forword it and I have this error:


00:01:27,643 [ERROR] Uncaught exception escaped
java.lang.ClassCastException: test.EnumTest cannot be cast to com.extjs.gxt.ui.client.data.ModelData at com.extjs.gxt.ui.client.widget.form.ComboBox.setValue(ComboBox.java:1) at com.extjs.gxt.ui.client.widget.Editor.startEdit(Editor.java:446) at com.extjs.gxt.ui.client.widget.grid.EditorSupport.deferStartEditing(EditorSupport.java:217) at com.extjs.gxt.ui.client.widget.grid.EditorSupport$2.run(EditorSupport.java:148) at com.google.gwt.user.client.Timer.fire(Timer.java:149) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337) at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) at com.google.gwt.core.client.impl.Impl.apply(Impl.java) at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213) at sun.reflect.GeneratedMethodAccessor65.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363) at java.lang.Thread.run(Thread.java:662)

Thanks

smiletolead
2 Feb 2012, 6:31 PM
Change

final SimpleComboBox<EnumTest>permissionComboBox = new SimpleComboBox<EnumTest>();


to

final SimpleComboBox<String> permissionComboBox = new SimpleComboBox<String>();


Because you are adding strings to the combo box in the following lines.

listirer
3 Feb 2012, 12:11 AM
Sorry, that is a typo, I have correct the previous post.

smiletolead
3 Feb 2012, 12:32 AM
Then make sure that EnumTest is a type of ModelData. You can do this by making EnumTest extend BaseModelData(which implements ModelData).

listirer
3 Feb 2012, 12:48 AM
TestDetail extends BeanModel, and EnumTest is a property of TestDetail.
This works in my (little) experience when I add a combo into a form.
Here, I add the combo into a CellEditor and the latter into a ColumnConfig.
So setId method maybe produce this problem...

smiletolead
3 Feb 2012, 4:27 AM
Please post EnumTest class. It might be of some help

smiletolead
3 Feb 2012, 5:29 AM
The cell editor you have used for the 'combo box' column does not override preProcessValue() and postProcessValue() methods. Refer the GXT examples. This is the reason you are getting those exceptions.

The method preProcessValue() is called before combobox appears in the grid. This method retrieves the model corresponding to the value appearing in the cell which is being edited.

The method postProcessValue() is called after the editing is complete. In this method, you return the value from the selected combobox model. The returned value will be shown in the grid cell.

listirer
3 Feb 2012, 5:49 AM
smiletolead thanks for the support!
I have fixed:


CellEditor comboEditor = new CellEditor(permissionComboBox) {

@Override
public Object preProcessValue(Object value) {
if (value == null) {
return value;
}
return permissionComboBox.findModel((EnumTest) value);
}

@Override
public Object postProcessValue(Object value) {
if (value == null) {
return value;
}
return ((SimpleComboValue<EnumTest>) value).getValue();
}
};