PDA

View Full Version : Best Practice for getting the value of a clicked cell in a grid



databass
7 May 2009, 2:38 PM
I have a listener which returns the values of each cell of a record clicked on a grid.

The grid is bound to a store which contains a list of LabelValue objects. The LabelValue class consists of two getters: getLabel() and getValue().

What I'm doing is working but it seems way too verbose. I'm still new at this...could someone possibly tell me if there's an easier way to do this?

Thanks....

Here's my code for the listener:

accountDetailsBasicGrid.addListener(Events.CellClick, new Listener<GridEvent<ModelData>>() {

public void handleEvent(GridEvent ge) {

LabelValue labelValueRecord = (LabelValue) ((Grid) ge.getSource()).getStore().getAt(ge.getRowIndex());

Window.alert("clicked label/value " + labelValueRecord.getLabel() + "/" + labelValueRecord.getValue());

}
});

micgala
8 May 2009, 5:53 AM
If I understood correctly, LabelValue is your Model class.

You can change your code to this:


accountDetaisBasicGrid.addListener(Events.CellClick, new Listener<GridEvent<LabelValue>>() {
public void handleEvent(GridEvent<LabelValue> be) {
LabelValue model = be.getModel();
Window.alert("clicked label/value " + model.getLabel() + "/" + model.getValue());
}
});Regards,
Michel.

databass
8 May 2009, 10:05 AM
Thanks for your reply. Yes, Label/Value is the Model class.

I tried your suggestion, but this gives me a null pointer exception when I click on a value.



[ERROR] Uncaught exception escaped
java.lang.NullPointerException: null
at com.ssi.enquesta.gwt.inquiry.client.component.AccountDetailsComponent$1.handleEvent(AccountDetailsComponent.java:105)
at com.ssi.enquesta.gwt.inquiry.client.component.AccountDetailsComponent$1.handleEvent(AccountDetailsComponent.java:1)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:100)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:455)
at com.extjs.gxt.ui.client.widget.grid.Grid.onClick(Grid.java:568)
at com.extjs.gxt.ui.client.widget.grid.Grid.onComponentEvent(Grid.java:366)
at com.extjs.gxt.ui.client.widget.Component.onBrowserEvent(Component.java:750)
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1320)
at com.google.gwt.user.client.DOM.dispatchEventAndCatch(DOM.java:1299)
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1262)
at sun.reflect.GeneratedMethodAccessor53.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.ie.IDispatchImpl.callMethod(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke(IDispatchProxy.java:155)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.SwtHostedModeBase.processEvents(SwtHostedModeBase.java:235)
at com.google.gwt.dev.HostedModeBase.pumpEventLoop(HostedModeBase.java:558)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:405)
at com.google.gwt.dev.GWTShell.main(GWTShell.java:140)


If I understood correctly, LabelValue is your Model class.

You can change your code to this:


accountDetaisBasicGrid.addListener(Events.CellClick, new Listener<GridEvent<LabelValue>>() {
public void handleEvent(GridEvent<LabelValue> be) {
LabelValue model = be.getModel();
Window.alert("clicked label/value " + model.getLabel() + "/" + model.getValue());
}
});Regards,
Michel.

sven
10 May 2009, 6:35 AM
That will be fixed within the next release.

databass
10 May 2009, 8:52 AM
Thanks. I'll stick with my workaround until then.