How to load json data to a table in java
Package com. google. gwt. sample. hello. client;
Import java. util. ArrayList;
Import java. util. List;
Import com. extjs. gxt. ui. client. Style. HorizontalAlignment;
Import com. extjs. gxt. ui. client. data. BaseListLoader;
Import com. extjs. gxt. ui. client. data. HttpProxy;
Import com. extjs. gxt. ui. client. data. JsonLoadResultReader;
Import com. extjs. gxt. ui. client. data. ListLoadResult;
Import com. extjs. gxt. ui. client. data. ModelData;
Import com. extjs. gxt. ui. client. data. ModelType;
Import com. extjs. gxt. ui. client. event. ButtonEvent;
Import com. extjs. gxt. ui. client. event. SelectionListener;
Import com. extjs. gxt. ui. client. store. ListStore;
Import com. extjs. gxt. ui. client. widget. ContentPanel;
Import com. extjs. gxt. ui. client. widget. LayoutContainer;
Import com. extjs. gxt. ui. client. widget. button. Button;
Import com. extjs. gxt. ui. client. widget. grid. ColumnConfig;
Import com. extjs. gxt. ui. client. widget. grid. ColumnModel;
Import com. extjs. gxt. ui. client. widget. grid. Grid;
Import com. extjs. gxt. ui. client. widget. layout. FitLayout;
Import com. extjs. gxt. ui. client. widget. layout. FlowLayout;
Import com. google. gwt. core. client. EntryPoint;
Import com. google. gwt. core. client. GWT;
Import com. google. gwt. http. client. RequestBuilder;
Import com. google. gwt. user. client. Element;
Import com. google. gwt. user. client. ui. RootPanel;
Public class Hello implements EntryPoint {
Public void onModuleLoad (){
RootPanel. get (). add (new JsonGridExample ());
}
}
Class JsonGridExample extends LayoutContainer {
@ Override
Protected void onRender (Element parent, int index ){
Super. onRender (parent, index );
SetLayout (new FlowLayout (10 ));
List <ColumnConfig> columns = new
ArrayList <ColumnConfig> ();
Columns. add (new ColumnConfig ("Sender", "Sender ",
100 ));
Columns. add (new ColumnConfig ("Email", "Email", 165 ));
Columns. add (new ColumnConfig ("Phone", "Phone", 100 ));
Columns. add (new ColumnConfig ("State", "State", 50 ));
Columns. add (new ColumnConfig ("Zip", "Zip Code", 65 ));
// Create the column model
ColumnModel cm = new ColumnModel (columns );
// Defines the xml structure
ModelType type = new ModelType ();
Type. setRoot ("records ");
Type. addField ("Sender", "name ");
Type. addField ("Email", "email ");
Type. addField ("Phone", "phone ");
Type. addField ("State", "state ");
Type. addField ("Zip", "zip ");
// Use a http proxy to get the data
RequestBuilder builder = new RequestBuilder
(RequestBuilder. GET, GWT. getHostPageBaseURL () +
"Data/data. json ");
HttpProxy <String> proxy = new HttpProxy <String>
(Builder );
// Need a loader, proxy, and reader
JsonLoadResultReader <ListLoadResult <ModelData> reader
= New JsonLoadResultReader <ListLoadResult <ModelData>
(Type );
Final BaseListLoader <ListLoadResult <ModelData> loader
= New BaseListLoader <ListLoadResult <ModelData> (proxy,
Reader );
ListStore <ModelData> store = new ListStore <ModelData>
(Loader );
Final Grid <ModelData> grid = new Grid <ModelData>
(Store, cm );
Grid. setBorders (true );
Grid. setLoadMask (true );
Grid. getView (). setEmptyText ("Please hit the load
Button .");
Grid. setAutoExpandColumn ("Sender ");
ContentPanel = new ContentPanel ();
Panel. setFrame (true );
Panel. setCollaps tutorial visible (true );
Panel. setAnimCollapse (false );
Panel. setButtonAlign (HorizontalAlignment. CENTER );
// Panel. setIcon (Resources. ICONS. table ());
Panel. setHeading ("JSON Table Demo ");
Panel. setLayout (new FitLayout ());
Panel. add (grid );
Panel. setSize (575,350 );
// Add buttons
Button load = new Button ("Load JSON", new
SelectionListener <ButtonEvent> (){
Public void componentSelected (ButtonEvent ce ){
Loader. load ();
}
});
Panel. addButton (load );
Add (panel );
}
}