首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SmartGWT ListGrid决赛

SmartGWT ListGrid决赛
EN

Stack Overflow用户
提问于 2013-05-01 06:49:52
回答 1查看 991关注 0票数 1

我意识到ListGrid必须永远是最终的。对我来说,静态对象是最好的,因为我想从另一个类修改它的属性。

在我的项目中,为了有一个清晰的代码,我创建了几个类。

  1. DataGrid extends ListGrid,它设置新对象的属性并填充数据。@重写方法将按钮添加到网格对象中。
  2. PopupWindow extend Window,它用于在单击ListGrid中的编辑按钮时创建一个窗口对象。在窗口中有一些文本框,您可以在其中添加新数据和提交按钮。Submit按钮的OnClick事件将将数据写入MySQL服务器,应该使用实际的数据(来自MySQL的查询)更新网格。这是我无法实现的部分。
  3. 在入口点类onModuleLoad中,我只有以下网格代码: DataGrid grid_far =新的DataGrid();grid_far.setGridData();

我是java中的新手,也许我不应该使用这么多类,只需将所有内容都放在入口点类onModuleLoad()中即可。

如果在PopupWindow扩展窗口类中,我声明Button OnClick从Entry Point类运行onModuleLoad() methode,这会复制我的网页吗?我是说这么做

代码语言:javascript
复制
 EntryPointClass ep = new EntryPointClass();
 ep.onModuleLoad();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-01 21:04:20

  • "ListGrid必须永远是最终的“

这是不必要的。可以将ListGrid变量创建为非最终变量。

您一定看到过一些示例,其中ListGrid变量被声明为最终变量,但由于其他原因。

例如,不可能在匿名内部类中使用非最终局部变量(在方法中声明的局部变量)。

因此,为了从内部类访问局部变量,需要将它们声明为最终变量。

在SmartGWT/Swing/等等中,内部类用于实现各种回调功能,如事件处理。

代码语言:javascript
复制
public class Screen {

    ListGrid grid1 = new ListGrid();
    TextItem text1 = new TextItem("text1", "Text 1");

    public void initialize() {
        // normally its not required to create subclasses of ListGrid/Button/Window/etc.
        // unless a significant change in their behavior is needed

        ListGrid grid2 = new ListGrid();
        // setup grid properties
        // set grid fields

        TextItem text2 = new TextItem("text2", "Text 2");

        final ListGrid grid3 = new ListGrid();
        final TextItem text3 = new TextItem("text3", "Text 3");

        IButton button = new IButton("Edit");
        button.addClickHandler(new ClickHandler() { // this is declaring an anonymous inner class
            public void onClick(ClickEvent clickEvent) { // this method is a member of that anonymous inner class
                // out of three ListGrid and thee TextItem instances, only following can be accessed in this method/class
                //   grid1, text1 (these are not local variables, inner class can access outer class members without any issue)
                //   grid3, text3 (as they are final, even though local variables)
            }
        });

        // that does not mean, grid2 and text2 can not be used, they can be, just not inside an anonymous inner class
        // e.g.-
        DynamicForm form = new DynamicForm();
        form.setFields(text2);

        VLayout layout = new VLayout();
        layout.addMember(grid2);
    }
}

有关在内部类中使用局部变量的详细信息,请参阅以下链接

内部类和局部变量

Java中的局部变量问题

  • “静态对象是最好的,因为我想从另一个类修改它的属性”

在对象之间有比使用静态变量更好的通信方式。

  • “也许我不应该使用这么多的类,只需将所有内容都放在入口点类onModuleLoad()中即可。”

最好将onModuleLoad()中的代码保持在最小值。

所需类的数量取决于您试图实现什么。

  • "EntryPoint“

您不能删除EntryPoint实现,因为这是GWT将切换执行以创建应用程序的地方。

onModuleLoad()由GWT/JavaScript引擎为此调用。

您的代码不能调用它。

查看SmartGWT橱窗,包括代码示例。

有关更多细节,请参阅SmartGWT API

创建UI的方法有多种,以实现相同的结果。

与服务器通信以在SmartGWT中发送/接收数据是其本身的一个主题。

可能的实施准则。

代码语言:javascript
复制
public class EntryPointClass implements EntryPoint {
    public void onModuleLoad() {
        ApplicationScreen screen = new ApplicationScreen();

        HStack drawArea = new HStack();
        drawArea.setWidth100();
        drawArea.setHeight100();
        drawArea.addMember(screen.getComponents());
        drawArea.draw();
    }
}

public class ApplicationScreen { // this class does not need to extend from a widget
    public Canvas getComponents() {
        // a method that prepares the interface
        // using a from+grid type layout, without a popup window

        ListGrid grid = getListGrid();

        DynamicForm form = getDynamicForm(grid); // have to pass grid in order to add/update records on button events

        VLayout layout = new VLayout();
        layout.addMember(form);
        layout.addMember(grid);

        return layout;
    }

    private DynamicForm getDynamicForm(final ListGrid grid) { // have to declare grid as final to access from event handler inner classes
        final TextItem text1 = new TextItem("text1", "Text 1");  // have to declare as final for same reason

        ButtonItem saveButton = new ButtonItem("Save");
        saveButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                // use text1, grid and other components to save form values and refresh grid
            }
        });

        // creating and configuring form
        DynamicForm form = new DynamicForm();
        form.setWidth100();
        form.setFields(text1, saveButton);

        return form;
    }

    private ListGrid getListGrid() {
        // preparing grid fields
        ListGridField field1 = new ListGridField("field1", "Field 1");

        // creating and configuring grid
        ListGrid grid = new ListGrid(); // not final, does not need to be
        grid.setWidth100();
        grid.setFields(field1);

        return grid;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16313936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档