GWT - Widget FormPanel
Introdução
o FormPanel widget representa um painel que envolve seu conteúdo em um elemento HTML <FORM>.
Declaração de Classe
A seguir está a declaração para com.google.gwt.user.client.ui.FormPanel classe -
public class FormPanel
extends SimplePanel
implements FiresFormEvents,
com.google.gwt.user.client.ui.impl.FormPanelImplHost
Construtores de classe
Sr. Não. | Construtor e descrição |
---|---|
1 |
FormPanel() Cria um novo FormPanel. |
2 |
protected FormPanel(Element element) Este construtor pode ser usado por subclasses para usar explicitamente um elemento existente. |
3 |
protected FormPanel(Element element, boolean createIFrame) Este construtor pode ser usado por subclasses para usar explicitamente um elemento existente. |
4 |
FormPanel(NamedFrame frameTarget) Cria um FormPanel que tem como alvo um NamedFrame. |
5 |
FormPanel(java.lang.String target) Cria um novo FormPanel. |
Métodos de aula
Sr. Não. | Nome e descrição da função |
---|---|
1 |
void add Form Handler (FormHandler handler) Descontinuada. Use adicionar Send Complete Handler (com.google.gwt.user.client.ui.Form Panel.Submit Complete Handler) e adicione Enviar Handler (com.google.gwt.user.client.ui.Form Panel.Submit Handler). |
2 |
Handler Registration addSubmit Complete Handler (FormPanel.SubmitCompleteHandler handler) Adiciona um manipulador de eventos FormPanel.Submit Complete. |
3 |
HandlerRegistration addSubmitHandler(FormPanel.SubmitHandler handler) Adiciona um manipulador FormPanel.SubmitEvent. |
4 |
java.lang.String getAction() Obtém a 'ação' associada a este formulário. |
5 |
java.lang.String getEncoding() Obtém a codificação usada para enviar este formulário. |
6 |
java.lang.String getMethod() Obtém o método HTTP usado para enviar este formulário. |
7 |
java.lang.String getTarget() Obtém o 'destino' do formulário. |
8 |
protected void onAttach() Este método é chamado quando um widget é anexado ao documento do navegador. |
9 |
protected void onDetach() Este método é chamado quando um widget é desanexado do documento do navegador. |
10 |
boolean onFormSubmit() Disparado quando um formulário é enviado. |
11 |
void onFrameLoad() |
12 |
void removeFormHandler(FormHandler handler) Descontinuada. Use o método HandlerRegistration.removeHandler () no objeto retornado por e adicione * Método Handler em seu lugar |
13 |
void reset() Reinicia o formulário, limpando todos os campos. |
14 |
void setAction(java.lang.String url) Define a 'ação' associada a este formulário. |
15 |
void setEncoding(java.lang.String encodingType) Define a codificação usada para enviar este formulário. |
16 |
void setMethod(java.lang.String method) Define o método HTTP usado para enviar este formulário. |
17 |
void submit() Envia o formulário. |
18 |
static FormPanel wrap(Element element) Cria um FormPanel que envolve um elemento <form> existente. |
19 |
static FormPanel wrap(Element element, boolean createIFrame) Cria um FormPanel que envolve um elemento <form> existente. |
Métodos herdados
Esta classe herda métodos das seguintes classes -
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
Exemplo de widget FormPanel
Este exemplo o levará por etapas simples para mostrar o uso de um FormPanel Widget no GWT. Siga as etapas a seguir para atualizar o aplicativo GWT que criamos no GWT - capítulo Criar aplicativo -
Degrau | Descrição |
---|---|
1 | Crie um projeto com o nome HelloWorld em um pacote com.tutorialspoint conforme explicado no capítulo GWT - Criar aplicativo . |
2 | Modifique HelloWorld.gwt.xml , HelloWorld.css , HelloWorld.html e HelloWorld.java conforme explicado abaixo. Mantenha o resto dos arquivos inalterados. |
3 | Compile e execute o aplicativo para verificar o resultado da lógica implementada. |
A seguir está o conteúdo do descritor do módulo modificado src/com.tutorialspoint/HelloWorld.gwt.xml.
<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name = 'com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. -->
<inherits name = 'com.google.gwt.user.theme.clean.Clean'/>
<!-- Specify the app entry point class. -->
<entry-point class = 'com.tutorialspoint.client.HelloWorld'/>
<!-- Specify the paths for translatable code -->
<source path = 'client'/>
<source path = 'shared'/>
</module>
A seguir está o conteúdo do arquivo de folha de estilo modificado war/HelloWorld.css.
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;
}
A seguir está o conteúdo do arquivo host HTML modificado war/HelloWorld.html.
<html>
<head>
<title>Hello World</title>
<link rel = "stylesheet" href = "HelloWorld.css"/>
<script language = "javascript" src = "helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>FormPanel Widget Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
Vamos ter o seguinte conteúdo do arquivo Java src/com.tutorialspoint/HelloWorld.java que demonstrará o uso do widget FormPanel.
package com.tutorialspoint.client;
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 HelloWorld 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();
panel.setSpacing(10);
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setWidth("220");
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("item1", "item1");
lb.addItem("item2", "item2");
lb.addItem("item3", "item3");
lb.setWidth("220");
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 ClickHandler() {
@Override
public void onClick(ClickEvent event) {
form.submit();
}
}));
// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
@Override
public void onSubmit(SubmitEvent 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.cancel();
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent 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
// here.
Window.alert(event.getResults());
}
});
DecoratorPanel decoratorPanel = new DecoratorPanel();
decoratorPanel.add(form);
// Add the widgets to the root panel.
RootPanel.get().add(decoratorPanel);
}
}
Quando você estiver pronto com todas as alterações feitas, vamos compilar e rodar o aplicativo em modo de desenvolvimento como fizemos no capítulo GWT - Criar Aplicativo . Se tudo estiver bem com sua aplicação, isso produzirá o seguinte resultado -