Google Web Toolkit (Beta)
Search this site:

# Class FormPanel

public class FormPanel
extends SimplePanel
implements FiresFormEvents, FormPanelImplHost
A panel that wraps its contents in an HTML <FORM> element.

This panel can be used to achieve interoperability with servers that accept traditional HTML form encoding. The following widgets (those that implement HasName) will be submitted to the server if they are contained within this panel:

- TextBox
- PasswordTextBox
- RadioButton
- CheckBox
- TextArea
- ListBox
- FileUpload
- Hidden

In particular, FileUpload is only useful when used within a FormPanel, because the browser will only upload files using form submission.

### Example

    public class FormPanelExample implements EntryPoint {

      public void onModuleLoad() {
        // Create a FormPanel and point it at a service.
        final FormPanel form = new FormPanel();
        form.setAction("/myFormHandler");

        // Because we're going to add a FileUpload widget, we'll need to set the
        // form to use the POST method, and multipart MIME encoding.
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setMethod(FormPanel.METHOD_POST);

        // Create a panel to hold all of the form widgets.
        VerticalPanel panel = new VerticalPanel();
        form.setWidget(panel);

        // Create a TextBox, giving it a name so that it will be submitted.
        final TextBox tb = new TextBox();
        tb.setName("textBoxFormElement");
        panel.add(tb);

        // Create a ListBox, giving it a name and some values to be associated with
        // its options.
        ListBox lb = new ListBox();
        lb.setName("listBoxFormElement");
        lb.addItem("foo", "fooValue");
        lb.addItem("bar", "barValue");
        lb.addItem("baz", "bazValue");
        panel.add(lb);

        // Create a FileUpload widget.
        FileUpload upload = new FileUpload();
        upload.setName("uploadFormElement");
        panel.add(upload);

        // Add a 'submit' button.
        panel.add(new Button("Submit", new ClickListener() {
          public void onClick(Widget sender) {
            form.submit();
          }
        }));

        // Add an event handler to the form.
        form.addFormHandler(new FormHandler() {
          public void onSubmit(FormSubmitEvent event) {
            // This event is fired just before the form is submitted. We can take
            // this opportunity to perform validation.
            if (tb.getText().length() == 0) {
              Window.alert("The text box must not be empty");
              event.setCancelled(true);
            }
          }

          public void onSubmitComplete(FormSubmitCompleteEvent event) {
            // When the form submission is successfully completed, this event is
            // fired. Assuming the service returned a response of type text/html,
            // we can get the result text here (see the FormPanel documentation for
            // further explanation).
            Window.alert(event.getResults());
          }
        });

        RootPanel.get().add(form);
      }
    }

## Fields

ENCODING_MULTIPART | Used with setEncoding(String) to specify that the form will be submitted using MIME encoding (necessary for FileUpload to work properly).
ENCODING_URLENCODED | Used with setEncoding(String) to specify that the form will be submitted using traditional URL encoding.
METHOD_GET | Used with setMethod(String) to specify that the form will be submitted using an HTTP GET request.
METHOD_POST | Used with setMethod(String) to specify that the form will be submitted using an HTTP POST request (necessary for FileUpload to work properly).

## Constructors

FormPanel() | Creates a new FormPanel.
FormPanel(NamedFrame) | Creates a FormPanel that targets a NamedFrame.
FormPanel(String) | Creates a new FormPanel.

## Methods

addFormHandler(FormHandler) | Adds a handler interface to receive click events.
getAction() | Gets the 'action' associated with this form.
getEncoding() | Gets the encoding used for submitting this form.
getMethod() | Gets the HTTP method used for submitting this form.
getTarget() | Gets the form's 'target'.
onAttach() |
onDetach() |
onFormSubmit() | Called when the form is submitted.
onFrameLoad() | Called when the target frame is done loading.
removeFormHandler(FormHandler) | Removes a previously added handler interface.
setAction(String) | Sets the 'action' associated with this form.
setEncoding(String) | Sets the encoding used for submitting this form.
setMethod(String) | Sets the HTTP method used for submitting this form.
submit() | Submits the form.

## Field Detail

### ENCODING_MULTIPART

public static final String ENCODING_MULTIPART
Used with setEncoding(String) to specify that the form will be submitted using MIME encoding (necessary for FileUpload to work properly).

### ENCODING_URLENCODED

public static final String ENCODING_URLENCODED
Used with setEncoding(String) to specify that the form will be submitted using traditional URL encoding.

### METHOD_GET

public static final String METHOD_GET
Used with setMethod(String) to specify that the form will be submitted using an HTTP GET request.

### METHOD_POST

public static final String METHOD_POST
Used with setMethod(String) to specify that the form will be submitted using an HTTP POST request (necessary for FileUpload to work properly).

## Constructor Detail

### FormPanel

public FormPanel()
Creates a new FormPanel. When created using this constructor, it will be submitted to a hidden <iframe> element, and the results of the submission made available via FormHandler.

The back-end server is expected to respond with a content-type of 'text/html', meaning that the text returned will be treated as HTML. If any other content-type is specified by the server, then the result html sent in the onFormSubmit event will be unpredictable across browsers, and the FormHandler.onSubmitComplete(FormSubmitCompleteEvent) event may not fire at all.

### FormPanel

public FormPanel(NamedFrame frameTarget)
Creates a FormPanel that targets a NamedFrame. The target frame is not physically attached to the form, and must therefore still be added to a panel elsewhere.

When the FormPanel targets an external frame in this way, it will not fire the onFormSubmit event.

#### Parameters

frameTarget
  the NamedFrame to be targetted

### FormPanel

public FormPanel(String target)
Creates a new FormPanel. When created using this constructor, it will be submitted either by replacing the current page, or to the named <iframe>.

When the FormPanel targets an external frame in this way, it will not fire the onFormSubmit event.

#### Parameters

target
  the name of the <iframe> to receive the results of the submission, or `null` to specify that the current page be replaced

## Method Detail

### addFormHandler

public void addFormHandler(FormHandler handler)
Adds a handler interface to receive click events.

#### Parameters

handler
  the handler interface to add

### getAction

public String getAction()
Gets the 'action' associated with this form. This is the URL to which it will be submitted.

#### Return Value

the form's action

### getEncoding

public String getEncoding()
Gets the encoding used for submitting this form. This should be either ENCODING_MULTIPART or ENCODING_URLENCODED.

#### Return Value

the form's encoding

### getMethod

public String getMethod()
Gets the HTTP method used for submitting this form. This should be either METHOD_GET or METHOD_POST.

#### Return Value

the form's method

### getTarget

public String getTarget()
Gets the form's 'target'. This is the name of the NamedFrame that will receive the results of submission, or `null` if none has been specified.

#### Return Value

the form's target.

### onAttach

protected void onAttach()

### onDetach

protected void onDetach()

### onFormSubmit

public boolean onFormSubmit()
Called when the form is submitted.

#### Return Value

`false` to cancel the submit

### onFrameLoad

public void onFrameLoad()
Called when the target frame is done loading.

### removeFormHandler

public void removeFormHandler(FormHandler handler)
Removes a previously added handler interface.

#### Parameters

handler
  the handler interface to remove

### setAction

public void setAction(String url)
Sets the 'action' associated with this form. This is the URL to which it will be submitted.

#### Parameters

url
  the form's action

### setEncoding

public void setEncoding(String encodingType)
Sets the encoding used for submitting this form. This should be either ENCODING_MULTIPART or ENCODING_URLENCODED.

#### Parameters

encodingType
  the form's encoding

### setMethod

public void setMethod(String method)
Sets the HTTP method used for submitting this form. This should be either METHOD_GET or METHOD_POST.

#### Parameters

method
  the form's method

### submit

public void submit()
Submits the form.

The FormPanel must not be detached (i.e. removed from its parent or otherwise disconnected from a RootPanel) until the submission is complete. Otherwise, notification of submission will fail.