从GWT中的flexTable中删除行

时间:2012-07-10 14:33:59

标签: java gwt gwt2 flextable

我正在学习GWT

在flexTable的第一行中,我添加了组合框,组合框,文本框,AddButton,删除按钮。

在addButton的click事件中,第2行被添加到与第一个相同的组件的flexTable中。 (组合框,组合框,文本框,AddButton,删除按钮。)

等等。

现在我要做的就是编写一个代码来删除removeButton的click事件上的行

enter image description here 当点击任何添加按钮时,会添加类似的行。

现在我想要的是当我点击删除按钮同一行显示被删除

怎么做。


被修改

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;
    }

错误

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)

3 个答案:

答案 0 :(得分:4)

GWT Google Group开始,动态检索处理程序中rowIndex的最佳解决方案是使用flexTable.getCellForEvent(event)

remove.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        int rowIndex = flexTable.getCellForEvent(event).getRowIndex();
        flexTable.removeRow(rowIndex);
    }
});

答案 1 :(得分:2)

当您从“删除”按钮中捕获onClick事件时,您需要找出您所在的行,以便查找最近的行,您可以使用下面的函数(尊重您的单元格中没有表格“删除“按钮”:

 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;
}

在你的onClick中你可以得到rowId:

TableRowElement tableRow = findNearestParentRow(removeButton.getElement());
flexTable.removeRow(tableRow.getRowIndex());

答案 2 :(得分:0)

 butCe.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                display.setText(display.getText().replaceAll(display.getText(),""));
            }
        });