GWT CellTable

9599 단어 gwtgoogle자바
더 읽 기
package com.cn.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
/**
* Entry point classes define onModuleLoad() .
*/
public class CellTableTest implements EntryPoint {
/**
* A simple data type that represents a contact.
*/
private static class Contact {
private String address;
private Date birthday;
private String name;
private String firstName;
private String lastName;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Contact(String firstName, String lastName, Date birthday,
String address) {
this.firstName = firstName;
this.lastName = lastName;
this.name = firstName + ""+ lastName;
this.birthday = birthday;
this.address = address;
}
public static final ProvidesKey KEY_PROVIDER = new ProvidesKey() {
public Object getKey(Contact item) {
return item == null ? null : item.getName();
}
};
}
private Label selectedLabel = new Label();
public void onModuleLoad() {
//Create a CellTable.
final CellTable cellTable = new CellTable();
//Display n rows in one page
cellTable.setPageSize(10);
cellTable.setWidth("1024px");
//Add a selection model so we can select cells.
final MultiSelectionModel selectionModel = new MultiSelectionModel(
Contact.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager. createCheckboxManager());
selectionModel.addSelectionChangeHandler(
          new SelectionChangeEvent.Handler() {
            public void onSelectionChange(SelectionChangeEvent event) {
              StringBuilder sb = new StringBuilder();
              boolean first = true;
              List selected = new ArrayList(selectionModel.getSelectedSet());
              for (Contact value : selected) {
                if (first) {
                first = false;
                } else {
                sb.append(", ");
                }
                sb.append(value.getName());
              }
              selected Label. setText ("선택:"+ sb. toString ());
            }
          });
Column checkColumn = new Column(
new CheckboxCell(true, false)) {
@Override
public Boolean getValue(Contact object) {
return selectionModel.isSelected(object);
}
};
cellTable.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant(""));
cellTable.setColumnWidth(checkColumn, 10, Unit.PX);
Column firstNameColumn = new Column(
new EditTextCell()) {
@Override
public String getValue(Contact object) {
return object.getFirstName();
}
};
cellTable. addColumn (firstNameColumn, "이름");
firstNameColumn.setFieldUpdater(new FieldUpdater() {
public void update(int index, Contact object, String value) {
//Called when the user changes the value.
object.setFirstName(value);
}
});
cellTable.setColumnWidth(firstNameColumn, 10, Unit.PCT);
Column lastNameColumn = new Column(
new EditTextCell()) {
@Override
public String getValue(Contact object) {
return object.getLastName();
}
};
lastNameColumn.setSortable(true);
cellTable. addColumn (lastNameColumn, "성");
lastNameColumn.setFieldUpdater(new FieldUpdater() {
public void update(int index, Contact object, String value) {
object.setLastName(value);
}
});
cellTable.setColumnWidth(lastNameColumn, 10, Unit.PCT);
//Add a text column to show the name.
TextColumn nameColumn = new TextColumn() {
@Override
public String getValue(Contact object) {
return object.getFirstName() + ""+ object.getLastName();
}
};
cellTable. addColumn (nameColumn, "전체 이름");
cellTable.setColumnWidth(nameColumn, 20, Unit.PCT);
//Add a date column to show the birthday.
DateCell dateCell = new DateCell();
Column dateColumn = new Column(dateCell) {
@Override
public Date getValue(Contact object) {
return object.birthday;
}
};
cellTable. addColumn (dateColumn, "생일");
cellTable.setColumnWidth(dateColumn, 40, Unit.PCT);
//Add a text column to show the address.
TextColumn addressColumn = new TextColumn() {
@Override
public String getValue(Contact object) {
return object.address;
}
};
cellTable. addColumn (addressColumn, "주소");
cellTable.setColumnWidth(addressColumn, 50, Unit.PCT);
//Associate an async data provider to the cellTable
//XXX: Use AsyncCallback in the method onRangeChanged
//to actaully get the data from the server side
AsyncDataProvider provider = new AsyncDataProvider() {
@Override
protected void onRangeChanged(HasData display) {
//int start = display.getVisibleRange().getStart();
//int end = start + display.getVisibleRange().getLength();
//end = end >= CONTACTS.size() ? CONTACTS.size() : end;
//List sub = CONTACTS.subList(start, end);
List subs = new ArrayList();
Contact c1 = new Contact ("왕", "동", new Date (), "장가 구 하 화원 촌");
연락처 c2 = new Contact ("장", "강", new Date (), "장자 항 시 장자 항 촌");
Contact c3 = new Contact ("왕", "샤 오 펑", new Date (), "하북성 보 정시 보 련 등 촌");
Contact c4 = new Contact ("시", "밝 음", new Date (), "북경 시 해정 구");
Contact c5 = new Contact ("왕", "소 하", new Date (), "승 덕 시 조양 구");
연락처 c6 = new Contact ("리", "동 벽", new Date (), "북경 시 화원 촌");
연락처 c7 = new Contact ("조", "대담", new Date (), "북경 시 화원 촌");
연락처 c8 = new Contact ("장", "계 령", new Date (), "북경 시 화원 촌");
연락처 c9 = new Contact ("왕", "뚱보", new Date (), "북경 시 화원 촌");
Contact c10 = new Contact ("감사합니다", "자 양", new Date (), "북경 시 창 평 구 사가 둔");
Contact c11 = new Contact ("제", "전", new Date (), "베 이 징 시 하 이 뎬 구 서북 왕 촌");
연락처 c12 = new Contact ("초", "목", new Date (), "북경 시 아시 안 게임 촌");
Contact c13 = new Contact ("얼음", "구멍", new Date (), "북경 시 올림픽 촌");
subs.add(c1);
subs.add(c2);
subs.add(c3);
subs.add(c4);
subs.add(c5);
subs.add(c6);
subs.add(c7);
subs.add(c8);
subs.add(c9);
subs.add(c10);
subs.add(c11);
subs.add(c12);
subs.add(c13);
updateRowData(0, subs);
}
};
provider.addDataDisplay(cellTable);
//provider.updateRowCount(CONTACTS.size(), true);
SimplePager pager = new SimplePager();
pager.setDisplay(cellTable);
VerticalPanel vp = new VerticalPanel();
HorizontalPanel hp = new HorizontalPanel();
버튼 cellTableButton = new 단추 ("제출");
cellTableButton.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
String select = selectedLabel.getText();
Window.alert(select);
/*
String[] selectArray = select.split(",");
if(selectArray!=null && selectArray.length>0){
for(int i=0;i Window.alert(selectArray[i]);
}
}
*/
}
});
hp.add(selectedLabel);
hp.add(cellTableButton);
vp.add(hp);
vp.setCellHorizontalAlignment(hp, HorizontalPanel.ALIGN_RIGHT);
vp.add(cellTable);
vp.add(pager);
vp.setCellHorizontalAlignment(pager, HorizontalPanel.ALIGN_CENTER);
//Add it to the root panel.
RootPanel.get().add(vp);
}
}

좋은 웹페이지 즐겨찾기