SmartGWT ListGrid最终版

时间:2013-05-01 06:49:52

标签: java static smartgwt final listgrid

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

在我的项目中有一个明确的代码我创建了几个类。

  1. DataGrid extends ListGrid,用于设置新对象的属性并填充数据。 @Override方法将按钮添加到我的网格对象。

  2. PopupWindow extend Window,用于在单击ListGrid中的编辑按钮时创建Window对象。在窗口中有一些文本框,您可以在其中添加新数据和“提交”按钮。 “提交”按钮的OnClick事件将数据写入MySQL服务器,应使用实际数据更新网格(来自MySQL的查询)。这是我无法实施的部分。

  3. 在入口点类onModuleLoad中,我只有这个网格代码:

    final DataGrid grid_far = new DataGrid(); grid_far.setGridData();

  4. 我是java的新手,也许我不应该使用这么多的类,只需将所有内容放在入口点类onModuleLoad()上?

    如果在PopupWindow扩展Window类我声明Button OnClick从Entry Point类运行onModuleLoad()方法,这会复制我的网页吗? 我的意思是这样做:

     EntryPointClass ep = new EntryPointClass();
     ep.onModuleLoad();
    

1 个答案:

答案 0 :(得分:0)

  • “ListGrid必须始终为最终”

这不是必需的。可以将ListGrid变量创建为非final 您必须看到ListGrid变量被声明为final的样本,但出于其他原因。

例如,它不可能在匿名内部类中使用非最终局部变量(在方法中声明的变量)。
因此,为了从内部类访问局部变量,需要将它们声明为final 在SmartGWT / Swing /等。内部类用于实现各种回调功能,如事件处理。

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

检查以下链接,了解有关在内部类中使用局部变量的更多详细信息 Inner class and local variables
Question about local final variable in Java

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

与使用静态变量相比,有更好的方法在对象之间进行通信。

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

最好将onModuleLoad()中的代码保持在最低限度 所需的类数取决于您尝试实现的内容。

  • “入口点”

您无法删除EntryPoint实施,因为GWT将切换执行以创建应用程序 而onModuleLoad()由GWT / JavaScript引擎调用。
它不能被你的代码调用。

浏览SmartGWT showcase包含代码示例 有关详细信息,请参阅SmartGWT API 有多种方法可以创建UI以实现相同的结果。

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

可能的实施指南。

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;
    }
}
相关问题