javastaticsmartgwtfinallistgrid

SmartGWT ListGrid final


I realized that ListGrid must be always Final. For me a static object would be the best because I would like to modify its properties from another class.

In my project to have a clear code I created several classes.

  1. class DataGrid extends ListGrid which set up the properties of the new object and also fills up with data. An @Override method adds button to my grid object.

  2. class PopupWindow extend Window which is used to create a Window object when you click on the edit button in the ListGrid. In the Window there are some textboxes where you can add new data and a Submit button. OnClick event of the Submit button will write the data to a MySQL server, and should update the grid with the actual data (query from the MySQL). This is the part I can not implement.

  3. In the Entry Point class onModuleLoad I just have this grid code:

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

I am new in java, maybe I should not use so many classes, just put everything in the Entry Point class onModuleLoad()?

If in the PopupWindow extends Window class I declare Button OnClick to run onModuleLoad() methode from Entry Point class, will this duplicate my web page? I mean doing this:

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

Solution

  • This is not necessary. Its possible to create ListGrid variables as non-final.
    You must have seen samples where ListGrid variables were declared as final, but its for some other reason.

    For example, its not possible to use a non-final local variable (one declared inside a method) in an anonymous inner class.
    So, in order to access local variables from an inner class, they need to be declared final.
    In SmartGWT/Swing/etc. inner classes are used to implement various callback functionalities like event handling.

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

    Check following links for more details on using local variables in inner classes
    Inner class and local variables
    Question about local final variable in Java

    There are better ways to communicate among objects than using static variables.

    Its better to keep code in onModuleLoad() to a minimum.
    Number of classes needed depend on what you try to implement.

    You can not remove EntryPoint implementation as that is where GWT will handover execution to create the application.
    And onModuleLoad() is called by GWT/JavaScript engine for that.
    It must not be called by your code.

    Go through the SmartGWT showcase including code samples.
    Refer SmartGWT API for more details.
    There are multiple ways to create the UI to achieve the same results.

    Communication with server to send/receive data in SmartGWT is a topic on its own.

    Possible implemention guideline.

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