Refresh Button in java
- by lakshmi
I need to make a simple refresh button  for a gui java program I have the button made but its not working properly. I just am not sure what the code needs to be to make the button work. Any help would be really appreciated.I figured the code below
` 
public class CompanySample extends Panel {
    DBServiceAsync dbService = GWT.create(DBService.class);
    Panel panel = new Panel();
public Panel CompanySampledetails(com.viji.example.domain.Record trec,
            int count) {
        panel.setWidth(800);
        Panel borderPanel = new Panel();
        Panel centerPanel = new Panel();
        final FormPanel formPanel = new FormPanel();
        formPanel.setFrame(true);
        formPanel.setLabelAlign(Position.LEFT);
        formPanel.setPaddings(5);
        formPanel.setWidth(800);
        final Panel inner = new Panel();
        inner.setLayout(new ColumnLayout());
        Panel columnOne = new Panel();
        columnOne.setLayout(new FitLayout());
        GridPanel gridPanel = null;
        if (gridPanel != null) {
            gridPanel.getView().refresh();
            gridPanel.clear();
        }
        gridPanel = new SampleGrid(trec);
        gridPanel.setHeight(450);
        gridPanel.setTitle("Company Data");
        final RowSelectionModel sm = new RowSelectionModel(true);
        sm.addListener(new RowSelectionListenerAdapter() {
            public void onRowSelect(RowSelectionModel sm, int rowIndex,
                    Record record) {
                formPanel.getForm().loadRecord(record);
            }
        });
        gridPanel.setSelectionModel(sm);
        gridPanel.doOnRender(new Function() {
            public void execute() {
                sm.selectFirstRow();
            }
        }, 10);
        columnOne.add(gridPanel);
        inner.add(columnOne, new ColumnLayoutData(0.6));
        final FieldSet fieldSet = new FieldSet();
        fieldSet.setLabelWidth(90);
        fieldSet.setTitle("company Details");
        fieldSet.setAutoHeight(true);
        fieldSet.setBorder(false);  
        final TextField txtcompanyname = new TextField("Name", "companyname",
                120);
        final TextField txtcompanyaddress = new TextField("Address",
                "companyaddress", 120);
        final TextField txtcompanyid = new TextField("Id", "companyid", 120);
        txtcompanyid.setVisible(false);
        fieldSet.add(txtcompanyid);
        fieldSet.add(txtcompanyname);
        fieldSet.add(txtcompanyaddress);
        final Button addButton = new Button();
        final Button deleteButton = new Button();
        final Button modifyButton = new Button();
        final Button refeshButton = new Button();
        addButton.setText("Add");
        deleteButton.setText("Delete");
        modifyButton.setText("Modify");
        refeshButton.setText("Refresh");
        fieldSet.add(addButton);
        fieldSet.add(deleteButton);
        fieldSet.add(modifyButton);
        fieldSet.add(refeshButton);
        final ButtonListenerAdapter buttonClickListener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
                if (button == refeshButton) {
                    sendDataToServ("Refresh");
                }
            }
        };
        addButton.addListener(new ButtonListenerAdapter() {
            @Override
            public void onClick(Button button, EventObject e) {
                if (button.getText().equals("Add")) {
                    sendDataToServer("Add");
                }
            }
            private void sendDataToServer(String action) {
                String txtcnameToServer = txtcompanyname.getText().trim();
                String txtcaddressToServer = txtcompanyaddress.getText().trim();
                if (txtcnameToServer.trim().equals("")
                        || txtcaddressToServer.trim().equals("")) {
                    Window
                            .alert("Incomplete Data. Fill/Select  all the needed fields");
                    action = "Nothing";
                }
                AsyncCallback ascallback = new AsyncCallback<String>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Record not inserted");
                    }
                    @Override
                    public void onSuccess(String result) {
                        Window.alert("Record inserted");
                    }
                };
                if (action.trim().equals("Add")) {
                    System.out.println("Before insertServer");
                    dbService.insertCompany(txtcnameToServer,
                            txtcaddressToServer, ascallback);
                }
            }
        });
        deleteButton.addListener(new ButtonListenerAdapter() {
            @Override
            public void onClick(Button button, EventObject e) {
                if (button.getText().equals("Delete")) {
                    sendDataToServer("Delete");
                }
            }
            private void sendDataToServer(String action) {
                String txtcidToServer = txtcompanyid.getText().trim();
                String txtcnameToServer = txtcompanyname.getText().trim();
                String txtcaddressToServer = txtcompanyaddress.getText().trim();
                if (!action.equals("Delete")) {
                    if (txtcidToServer.trim().equals("")
                            || txtcnameToServer.trim().equals("")
                            || txtcaddressToServer.trim().equals("")) {
                        Window
                                .alert("Incomplete Data. Fill/Select  all the needed fields");
                        action = "Nothing";
                    }
                } else if (txtcidToServer.trim().equals("")) {
                    Window.alert("Doesn't deleted any row");
                }
                AsyncCallback ascallback = new AsyncCallback<String>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                        Window.alert("Record not deleted");
                    }
                    @Override
                    public void onSuccess(String result) {
                        Window.alert("Record deleted");
                    }
                };
                if (action.trim().equals("Delete")) {
                    System.out.println("Before deleteServer");
                    dbService.deleteCompany(Integer.parseInt(txtcidToServer),
                            ascallback);
                }
            }
        });
        modifyButton.addListener(new ButtonListenerAdapter() {
            @Override
            public void onClick(Button button, EventObject e) {
                if (button.getText().equals("Modify")) {
                    sendDataToServer("Modify");
                }
            }
            private void sendDataToServer(String action) {
                // TODO Auto-generated method stub
                System.out.println("ACTION :----->" + action);
                String txtcidToServer = txtcompanyid.getText().trim();
                String txtcnameToServer = txtcompanyname.getText().trim();
                String txtcaddressToServer = txtcompanyaddress.getText().trim();
                if (txtcnameToServer.trim().equals("")
                        || txtcaddressToServer.trim().equals("")) {
                    Window
                            .alert("Incomplete Data. Fill/Select  all the needed fields");
                    action = "Nothing";
                }
                AsyncCallback ascallback = new AsyncCallback<String>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Record not Updated");
                    }
                    @Override
                    public void onSuccess(String result) {
                        Window.alert("Record Updated");
                    }
                };
                if (action.equals("Modify")) {
                    System.out.println("Before UpdateServer");
                    dbService.modifyCompany(Integer.parseInt(txtcidToServer),
                            txtcnameToServer, txtcaddressToServer, ascallback);
                }
            }
        });
        inner.add(new PaddedPanel(fieldSet, 0, 10, 0, 0), new ColumnLayoutData(
                0.4));
        formPanel.add(inner);
        borderPanel.add(centerPanel,
                new BorderLayoutData(RegionPosition.CENTER));
        centerPanel.add(formPanel);
        panel.add(borderPanel);
        return panel;
    }
    public void sendDataToServ(String action) {
        AsyncCallback<com.viji.example.domain.Record> callback = new AsyncCallback<com.viji.example.domain.Record>() {
            @Override
            public void onFailure(Throwable caught) {
                System.out.println("Failure");
            }
            public void onSuccess(com.viji.example.domain.Record result) {
                CompanySampledetails(result, 1);
            }
        };
        dbService.getRecords(callback);
    }
}
class SampleGrid extends GridPanel {
    private static BaseColumnConfig[] columns = new BaseColumnConfig[] {
            new ColumnConfig("Name", "companyname", 120, true),
            new ColumnConfig("Address", "companyaddress", 120, true),
    };
    private static RecordDef recordDef = new RecordDef(new FieldDef[] {
            new IntegerFieldDef("companyid"),
            new StringFieldDef("companyname"),
            new StringFieldDef("companyaddress") });
    public SampleGrid(com.viji.example.domain.Record trec) {
        Object[][] data = new Object[0][];// getCompanyDataSmall();
        MemoryProxy proxy = new MemoryProxy(data);
        ArrayReader reader = new ArrayReader(recordDef);
        Store store = new SimpleStore(new String[] { "companyid",
                "companyname", "companyaddress" }, new Object[0][]);
        store.load();
        ArrayList<Company> Companydetails = trec.getCompanydetails();
        for (int i = 0; i < Companydetails.size(); i++) {
            Company company = Companydetails.get(i);
            store.add(recordDef.createRecord(new Object[] {
                    company.getCompanyid(), company.getCompanyName(),
                    company.getCompanyaddress() }));
        }
        store.commitChanges();
        setStore(store);
        ColumnModel columnModel = new ColumnModel(columns);
        setColumnModel(columnModel);
    }
}
`