Java/GWT/FormPanel

Материал из Java эксперт
Версия от 06:35, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create a form with GWT controls

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
public class GWTClient implements EntryPoint{
   Label nameLabel = new Label("Name:");
   TextBox nameBox = new TextBox();
   Label addrLabel = new Label("Address:");
   TextBox addrBox = new TextBox();
   Label phoneLabel = new Label("Phone number:");
   TextBox phoneBox = new TextBox();
   Button button = new Button("Submit");
   Grid grid = new Grid(4, 2);
   
   public void onModuleLoad() {
      grid.setWidget(0, 0, nameLabel);
      grid.setWidget(0, 1, nameBox);
      grid.setWidget(1, 0, addrLabel);
      grid.setWidget(1, 1, addrBox);
      grid.setWidget(2, 0, phoneLabel);
      grid.setWidget(2, 1, phoneBox);
      grid.setWidget(3, 1, button);
      RootPanel.get().add(grid);
      
      button.addClickListener(new ClickListener() {
         public void onClick(Widget sender) {
            grid.setVisible(false);
            RootPanel.get().add(
                  new Label("Thanks for your submission."));
            Window.alert("Submit name=" + nameBox.getText()
                  + "\naddress=" + addrBox.getText() + "\nphone="
                  + phoneBox.getText());
         }
      });
   }
}





Form Panel for form control

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class GWTClient implements EntryPoint{
  public void onModuleLoad() {
    FormPanelExample e = new FormPanelExample();
    
    RootPanel.get().add(e);
  }
}
class FormPanelExample extends FormPanel {
    Grid grid = new Grid(3, 2);
    FileUpload upload = new FileUpload();
    TextBox textBox = new TextBox();
    Label textLabel = new Label("Name");;
    Label fileLabel = new Label("Upload Something");
    Button submit = new Button("Submit");
    public FormPanelExample() {
        super();
        textBox.setName("textBox");
        
        grid.setWidget(0, 0, textLabel);
        grid.setWidget(0, 1, textBox);
        
        upload.setName("upload");
        grid.setWidget(1, 0, fileLabel);
        grid.setWidget(1, 1, upload);
        
        submit.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    submit();
                }
        });
        grid.setWidget(2, 0, submit);
        setAction("/someAction");
        setEncoding(FormPanel.ENCODING_MULTIPART);
        setMethod(FormPanel.METHOD_POST);
        setWidget(grid);
        setStyleName("formPanel");
        addFormHandler(new FormHandler() {
                public void onSubmitComplete(FormSubmitCompleteEvent event) {
                    Window.alert(event.getResults());
                }
                public void onSubmit(FormSubmitEvent event) {
                }
        });
    }
}