45. 45
private static List quotes = new ArrayList();
static {
quotes.add("No great thing is created suddenly - Epictetus");
quotes.add("Well done is better than well said - Ben Franklin");
quotes.add("No wind favors he who has no destined port - Montaigne");
quotes.add("Sometimes even to live is an act of courage - Seneca");
quotes.add("Know thyself - Socrates");
}
public String myMethod(String s) {
return (String) quotes.get(randomizer.nextInt(5));
}
}
Listing ( sourcecode
5.2 # %onModuleLoad() (
? $#2
public void onModuleLoad() {
final Label quoteText = new Label();
Timer timer = new Timer() {
public void run() {
//create an async callback to handle the result:
AsyncCallback callback = new AsyncCallback() {
public void onFailure(Throwable arg0) {
//display error text if we can't get the quote:
quoteText.setText("Failed to get a quote");
}
public void onSuccess(Object result) {
//display the retrieved quote in the label:
quoteText.setText((String) result);
}
};
getService().myMethod(null, callback);
}
};
timer.scheduleRepeating(1000);
RootPanel.get().add(quoteText);
}
}
GWT : Client/Server Communication Thanachart Numnonda / Thanisa Kruawaisayawan
47. 47
6.2 ) MainEntryPoint.java
" 1 -* - #MainEntryPoint.java ! - %) ! &
(
' ?% 2 (
-
# % $Lookup - # 9- %) ! & Partial Screen Update % -
' :
( # " ?- ## 5(
% 2 ( #
% ! %
,
1. $ - #MainEntryPoint.java 1 % Listing (
* 6.2
2. " thaigwtapp "*) ! & 2
9 % ? % ( -
5.5
3. 5 deploy Google App Engine - % - # url (
" http://thaigwtapp
.appspot.com
Listing ( sourcecode
6.2 - #MainEntryPoint.java
package org.yournamehere.client;
import com.google.gwt.core.client.EntryPoint;
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.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.yournamehere.client.sampleservice.GWTService;
import org.yournamehere.client.sampleservice.GWTServiceAsync;
public class MainEntryPoint implements EntryPoint {
/**
* Creates a new instance of MainEntryPoint
*/
public MainEntryPoint() {
}
/**
* The entry point method, called automatically by loading a module
* that declares an implementing class as an entry-point
*/
public void onModuleLoad() {
InlineLabel label = new InlineLabel("Lookup meaning of Word");
final TextBox text = new TextBox();
// final TextArea ta = new TextArea();
// ta.setPixelSize(600, 400);
final HTML ta = new HTML();
Button bn = new Button("Lookup");
VerticalPanel panel = new VerticalPanel();
panel.add(label);
panel.add(text);
!, - #RIA ( Web Services Thanachart Numnonda / Thanisa Kruawaisayawan
48. 48
panel.add(bn);
panel.add(ta);
RootPanel.get().add(panel);
final AsyncCallback callback = new AsyncCallback() {
public void onFailure(Throwable arg0) {
//display error text if we can't get the quote:
ta.setHTML("Failed to get a quote");
}
public void onSuccess(Object result) {
//display the retrieved quote in the label:
ta.setHTML((String) result);
}
};
bn.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
getService().searchWord(text.getText(), callback);
}
});
}
public static GWTServiceAsync getService() {
return GWT.create(GWTService.class);
}
}
6.1 SearchWord
!, - #RIA ( Web Services Thanachart Numnonda / Thanisa Kruawaisayawan