Table of contents
1.
Introduction
2.
GWT FormPanel
3.
Class Declaration
4.
Class Constructors
5.
Class Methods
6.
Methods Inherited
7.
GWT FormPanel Example
7.1.
Code
7.2.
Output
8.
Frequently Asked Questions
8.1.
What is GWT FormPanel?
8.2.
How to get the HTTP method used to submit the form?
8.3.
What is the return type of the method onFrameLoad()?
8.4.
What is the function of the submit() method?
8.5.
How to get the action associated with the form?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

GWT FormPanel

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While developing web applications we sometimes need to design a panel that deals with the elements of HTML FORM in which any widget that would be included inside an HTML form element can be added.

gwt formpanel

 

In this article, we will discuss GWT FormPanel which can be used to create a panel that interacts with HTML form components such that any widget that would go within one may be added to it.

GWT FormPanel

Forms are typically used when you wish to gather user information. To purchase a bag, for instance, a user must first submit their mailing address in the address form and their payment information in the payment form.

The HTML FORM components are the subject of this Panel. Any widget inside an HTML form element can be added to this panel. In other words, a panel that encloses its contents in an HTML "FORM" element is represented by the GWT FormPanel widget.

Class Declaration

The declaration for the class com.google.gwt.user.client.ui.FormPanel is provided below.

public class FormPanel
   extends SimplePanel
      implements FiresFormEvents, 
         com.google.gwt.user.client.ui.impl.FormPanelImplHost
You can also try this code with Online Java Compiler
Run Code

Class Constructors

Following are the constructors provided by the FormPanel class:

Sr. No. Constructor  Description
1. FormPanel() Creates a new FormPanel.
2. protected FormPanel(Element element) Subclasses may use this constructor to utilize an existing element explicitly.
3. protected FormPanel(Element element, boolean createIFrame) Subclasses may use this constructor to utilize an existing element explicitly.
4. FormPanel(NamedFrame frameTarget) Creates a FormPanel with a NamedFrame as its target.
5. FormPanel(java.lang.String target) This constructor is used to create a new FormPanel.

Class Methods

Following are the methods provided by the FormPanel class:

Sr. No. Method Description
1. addSubmitCompleteHandler(FormPanel.Submit
CompleteHandler handler)
Inserts a FormPanel. Handler of the SubmitCompleteEvent.
2. addSubmitHandler(FormPanel.SubmitHandlet 
handler)
FormPanel is added. Handler for SubmitEvent
3. getAction() This method returns the ‘action’ associated with this form.
4.  getEncoding() This method returns the encoding used to submit this form.
5.  getMethod() This function returns the HTTP method used to submit this form.
6.  getTarget() Gets the ‘target’ of the form.
7.  onAttach() When a widget is added to the browser's page, this function is invoked.
8.  onDetach() When a widget is disconnected from the browser's page, this function is invoked.
9.  onFormSubmit() This event is triggered when a form is submitted.
10. onFrameLoad() This method is called when the target frame has finished loading.
11.  reset() The form is reset, and all fields are cleared.
12.  setAction(SafeUri URL) The ‘action’ associated with this form is set.
13. setAction(java.lang.String URL) This method is used to set the ‘action’ associated with this form.
14. setEncoding(java.lang.String encodingType) This method is used to set the encoding for this form's submission.
15. setMethod(java.lang.String method) This method is used to set the HTTP method used to submit this form.
16. submit() Used to submit the form.
17. wrap(Element element) This method generates a FormPanel that surrounds an existing <form> element.
18. wrap(Element element, boolean createFrame) This method is used to create a FormPanel that surrounds an existing <form> element.

Methods Inherited

This class inherits methods from the classes listed below.

  • com.google.gwt.user.client.ui.UIObject
  • com.google.gwt.user.client.ui.Widget
  • com.google.gwt.user.client.ui.Panel
  • com.google.gwt.user.client.ui.SimplePanel
  • java.lang.Object

GWT FormPanel Example

The example of implementing gwt formpanel is given below:

Create a SampleFormPanel.java file and paste the following code:

Code

import com.google.gwt.core.client.EntryPoint;  
import com.google.gwt.event.dom.client.ClickEvent;  
import com.google.gwt.event.dom.client.ClickHandler;  
import com.google.gwt.user.client.Window;  
import com.google.gwt.user.client.ui.Button;  
import com.google.gwt.user.client.ui.DecoratorPanel;  
import com.google.gwt.user.client.ui.FileUpload;  
import com.google.gwt.user.client.ui.FormPanel;  
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;  
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;  
import com.google.gwt.user.client.ui.ListBox;  
import com.google.gwt.user.client.ui.RootPanel;  
import com.google.gwt.user.client.ui.TextBox;  
import com.google.gwt.user.client.ui.VerticalPanel;  
  
public class FormPanelExample implements EntryPoint {  
  
  public void onModuleLoad() {  
    // Creating a FormPanel and point it at a service. 
     
    final FormPanel form = new FormPanel();  
    form.setAction("/myFormHandler");  
    form.setEncoding(FormPanel.ENCODING_MULTIPART);  
    form.setMethod(FormPanel.METHOD_POST);  
  
    // Creating a panel to hold all of the form widgets.  
    
    VerticalPanel panel = new VerticalPanel();  
    form.setWidget(panel);  
  
    // Creating a TextBox, giving it a name so that it will be submitted.  
    
    final TextBox tb = new TextBox();  
    tb.setName("textBoxFormElement");  
    panel.add(tb);  
  
    // Creating a ListBox, giving it a name and some values to be associated with its options.  
    
    ListBox lb = new ListBox();  
    lb.setName("listFormElement");  
    lb.addItem("Item1", "Item1Value");  
    lb.addItem("Item2", "Item2Value");  
    lb.addItem("Item3", "Item3Value");  
    panel.add(lb);  
  
    // Creating a FileUpload widget.  
    
    FileUpload upload = new FileUpload();  
    upload.setName("uploadFormElement");  
    panel.add(upload);  
  
    // Adding the 'submit' button.  
    
    panel.add(new Button("Submit", new ClickHandler() {  
      public void onClick(ClickEvent event) {  
        form.submit();  
      }  
    }));  
    
    form.addSubmitHandler(new FormPanel.SubmitHandler() {  
      public void onSubmit(SubmitEvent event) {  
   
        if (tb.getText().length() == 0) {  
          Window.alert("The text cannot be empty");  
          event.cancel();  
        }  
      }  
    });  
    
    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {  
      public void onSubmitComplete(SubmitCompleteEvent event) {  
          
        Window.alert(event.getResults());  
      }  
    });  
  
    RootPanel.get().add(form);  
  }  
}  
You can also try this code with Online Java Compiler
Run Code

 

Create a SampleFormPanel.css and paste the following code:

body {  
   text-align: center;  
   font-family: verdana, sans-serif;  
}  

h1 {  
   font-size: 2em;  
   font-weight: bold;  
   color: #777777;  
   margin: 40px 0px 70px;  
   text-align: center;  
}  

Output

The above codes will produce the output as shown below:

gwt formpanel

Frequently Asked Questions

What is GWT FormPanel?

A panel that encloses its contents in an HTML "FORM" element is represented by the FormPanel widget.

How to get the HTTP method used to submit the form?

The getMethod() method provided by the FormPanel class is used to get the HTTP method used to submit the form.

What is the return type of the method onFrameLoad()?

The return type of the method onFrameLoad is void.

What is the function of the submit() method?

This method is used to submit the form.

How to get the action associated with the form?

The method “getAction()” is used to get the action associated with the form.

Conclusion

In this article, we have extensively discussed the GWT FormPanel. After giving a quick overview of the GWT FormPanel, we spoke about how to put it into practice. Finally, a few frequently asked questions about the GWT FormPanel.

Do you not feel eager to read/explore additional information on the subject of GWT after reading about the GWT SplitLayoutPanel? See the GWT ScrollPanel Widget, GWT RootLayoutPanel, and GWT TabPanel Widget learn more.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

thank you image

Live masterclass