2011年4月5日火曜日

GWT UiBinder で FileUpload を使う





<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:style>

</ui:style>
<g:HTMLPanel>
<g:VerticalPanel>
<g:HTML><h2>Select a file:</h2></g:HTML>
<g:FileUpload ui:field="fileUpload"></g:FileUpload>
<g:HTML><br/></g:HTML>
<g:Button ui:field="button">Upload File</g:Button>
</g:VerticalPanel>
</g:HTMLPanel>
</ui:UiBinder>



package yanzm.example.hellowtgt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Widget;

public class FileUploadButton extends Composite {

private static FileUploadUiBinder uiBinder = GWT
.create(FileUploadUiBinder.class);

interface FileUploadUiBinder extends UiBinder<Widget, FileUploadButton> {
}

@UiField
FileUpload fileUpload;

@UiField
Button button;

public FileUploadButton() {
initWidget(uiBinder.createAndBindUi(this));

button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String filename = fileUpload.getFilename();
if(filename.length() == 0) {
Window.alert("No file selected");
}
else {
Window.alert("Start upload " + filename);
}
}
});
}
}





■ その他
Java code による実装例 (Showcase)
com.google.gwt.user.client.ui.FileUpload



 

0 件のコメント:

コメントを投稿