0

I am learning GWT

In in first row of flexTable i have added combobox, combobox , text box , AddButton , Remove button.

On click event of addButton 2 row is added to the flexTable withe same component as first. ( combobox, combobox , text box , AddButton , Remove button.)

ans so on.

now what i have to do is to write a code for removing the row on the click event of removeButton

enter image description here when click on any add button the similar row is added.

now what i want is when i click removebutton same row show be deleted

How to do that.


Edited

public class AdvancePropertySearch extends Composite{
// initialization
    public AdvancePropertySearch() {
        initWidget(getFlexTable());
    }

    private FlexTable getFlexTable() {
        if (flexTable == null) {
            flexTable = new FlexTable();
            flexTable.setWidth("1033px");
            flexTable.setHeight("100%");
            flexTable.setWidget(0, 0, getPropertyList());
            flexTable.setWidget(0, 1, getConditionList());
            flexTable.setWidget(0, 2, getKeyWord());
            flexTable.setWidget(0, 3, getAddbutton());
            flexTable.setWidget(0, 4, getRemove());
        }
        return flexTable;
    }
    private ListBox getPropertyList() {

            propertyList = new ListBox();
            propertyList.setWidth("297px");

        return propertyList;
    }
    private ListBox getConditionList() {

            conditionList = new ListBox();
            conditionList.setWidth("311px");

        return conditionList;
    }
    private TextBox getKeyWord() {

            keyWord = new TextBox();

        return keyWord;
    }
    private Button getAddbutton() {

            addbutton = new Button("");
            addbutton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    int i = flexTable.getRowCount();
                    flexTable.setWidget(i, 0, getPropertyList());
                    flexTable.setWidget(i, 1, getConditionList());
                    flexTable.setWidget(i, 2, getKeyWord());
                    flexTable.setWidget(i, 3, getAddbutton());

                    flexTable.setWidget(i, 4, getRemove());
                    remove.setStyleName("cmis-Button-removeOn");

                }
            });
            addbutton.setSize("25px", "25px");
            addbutton.setStyleName("cmis-Button-Add");

        return addbutton;
    }
    private Button getRemove() {

            remove = new Button("");
            remove.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {

                    TableRowElement tablerow = findNearestParentRow(remove.getElement());
                    flexTable.removeRow(tablerow.getRowIndex());
                }
            });
            remove.setSize("25px", "25px");
            remove.setStyleName("cmis-Button-removeOff");

        return remove;
    }

error

com.google.gwt.event.shared.UmbrellaException: One or more exceptions caught, see full set in UmbrellaException#getCauses
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)
    at com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)
    at com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116)
    at com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)
    at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)
    at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1307)
    at sun.reflect.GeneratedMethodAccessor49.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.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.GeneratedMethodAccessor24.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)
Caused by: com.google.gwt.dev.shell.HostedModeException: Something other than a short was returned from JSNI method '@com.google.gwt.dom.client.Node::getNodeType()': JS value of type undefined, expected short
    at com.google.gwt.dev.shell.JsValueGlue.getIntRange(JsValueGlue.java:266)
    at com.google.gwt.dev.shell.JsValueGlue.get(JsValueGlue.java:154)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeShort(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeShort(JavaScriptHost.java:99)
    at com.google.gwt.dom.client.Node$.getNodeType$(Node.java)
    at com.client.GUI.AdvancePropertySearch.findNearestParentNodeByType(AdvancePropertySearch.java:136)
    at com.client.GUI.AdvancePropertySearch.findNearestParentRow(AdvancePropertySearch.java:104)
    at com.client.GUI.AdvancePropertySearch$2.onClick(AdvancePropertySearch.java:90)
    at com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:54)
    at com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:1)
    at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
    at com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40)
    at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
    at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:127)
    at com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)
    at com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116)
    at com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)
    at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)
    at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1307)
    at sun.reflect.GeneratedMethodAccessor49.invoke(Unknown Source)
GameBuilder
  • 1,169
  • 4
  • 31
  • 62

3 Answers3

4

From GWT Google Group, the best solution to retrieve dynamically the rowIndex in a Handler is to use flexTable.getCellForEvent(event):

remove.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        int rowIndex = flexTable.getCellForEvent(event).getRowIndex();
        flexTable.removeRow(rowIndex);
    }
});
zigarn
  • 10,892
  • 2
  • 31
  • 45
2

When you catch onClick event from "remove" button you need to find out in which row you are so for finding the nearest row you may use a function below (with respect you don't have a table in your cell with "remove" button):

 public static TableRowElement findNearestParentRow(Node node)
{
    Node element = findNearestParentNodeByType(node, "tr");
    if (element != null)
    {
        return element.cast();
    }
    return null;
}

public static Node findNearestParentNodeByType(Node node, String nodeType)
{
    while ((node != null))
    {
        if (Element.is(node))
        {
            Element elem = Element.as(node);

            String tagName = elem.getTagName();

            if (nodeType.equalsIgnoreCase(tagName))
            {
                 return elem.cast();
            }

        }
        node = node.getParentNode();
    }
    return null;
}

In your onClick you can get the rowId:

TableRowElement tableRow = findNearestParentRow(removeButton.getElement());
flexTable.removeRow(tableRow.getRowIndex());
Alexey A.
  • 1,389
  • 1
  • 11
  • 20
  • String ... means we can pass several strings, this is done for such cases when you want to find not exact tag, but something like "td","th", but in your case you can omit that, and leave just single String argument. – Alexey A. Jul 11 '12 at 13:31
  • : ok.. when click on the removeButton the row is deleted. but when i click on another remove button its gies me error ` Caused by: com.google.gwt.dev.shell.HostedModeException: Something other than an int was returned from JSNI method '@com.google.gwt.dom.client.TableRowElement::getRowIndex()': JS value of type undefined, expected int` at `flexTable.removeRow(tablerow.getRowIndex());` – GameBuilder Jul 11 '12 at 13:44
  • there is an issue with debug mode in chrome so if this error is given sporadically, all will be working smooth once you compile your app. – Alexey A. Jul 11 '12 at 14:06
  • for testing I would recommend to use firefox it's gwt plugin is much more stable. – Alexey A. Jul 11 '12 at 14:06
  • i am using mozilla .. i have edited and added a code and stacktrace. plz have a look and help me. When i click on any removeButton the lastRow of the flex table is deleted. If i click 2nd time on any remove button. it gives me Error. – GameBuilder Jul 12 '12 at 10:11
  • I think I found the issue in the code. You use class variable button and in ClickHandler it uses the last added removeButton. So if you just change first line of code in getRemove() to `Button remove = new Button("");` all will be alright. – Alexey A. Jul 12 '12 at 11:00
  • to be precise `final Button remove = new Button("");` – Alexey A. Jul 12 '12 at 11:13
0
 butCe.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                display.setText(display.getText().replaceAll(display.getText(),""));
            }
        });
codebusta
  • 1,922
  • 1
  • 14
  • 15