Thursday, September 11, 2008

Google Web Toolkit Data Grid Example

At work I have a java J2EE application that displays a large table. The report is about 15 MB when rendered. I've been experimenting with GWT and I think it would be a good tool to rewrite this report with.

The main problem with the page is when someone updates a value it has to post back to the server then re-render everything. Using GWT it seems like it would be trivial to reload a single row at a time. Along those lines here is a simple DataGrid example that lets me add/remove rows.

See GWT Simple Example for more information on setting up your environment.


/*
* mcintoshEntryPoint.java
*
* Created on June 29, 2008, 1:46 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.client.time.*;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.Widget;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
*
* @author avalanche
*/
public class mcintoshEntryPoint implements EntryPoint {

final Button addButton = new Button("Add Row");
final Button deleteButton = new Button("Delete Row");
final FlexTable table = new FlexTable();
final Set selectedTableRows = new HashSet();

public mcintoshEntryPoint() {
addButton.addClickListener(new ClickListener() {

public void onClick(Widget arg0) {
table.insertRow(table.getRowCount());
table.addCell(table.getRowCount() - 1);
table.addCell(table.getRowCount() - 1);
table.setText(table.getRowCount() - 1, 1, "foo" + table.getRowCount());
}
});
deleteButton.addClickListener(new ClickListener() {

public void onClick(Widget arg0) {
Object[] array = selectedTableRows.toArray();
Arrays.sort(array);
for(int i=array.length-1;i>-1;i-- ) {
int row = ((Integer)array[i]).intValue();
table.removeRow(row);
selectedTableRows.remove(new Integer(row));
}
}
});
table.addTableListener(new TableListener() {

public void onCellClicked(SourcesTableEvents arg0, int rrow, int col) {
Integer row = new Integer(rrow);
if (selectedTableRows.contains(row)) {
selectedTableRows.remove(row);
table.setText(rrow, 0, "x");
} else {
selectedTableRows.add(row);
table.setText(rrow, 0, "y");
}
}
});


}

/**
* The entry point method, called automatically by loading a module
* that declares an implementing class as an entry-point
*/
public void onModuleLoad() {
RootPanel.get().add(table);
RootPanel.get().add(addButton);
RootPanel.get().add(deleteButton);
}
}

1 comment:

navindian said...

is an update for gwt 2.3 available?