Gwt-formpanel-widget

提供:Dev Guides
移動先:案内検索

GWT-FormPanelウィジェット

前書き

*FormPanel* ウィジェットは、そのコンテンツをHTML <FORM>要素でラップするパネルを表します。

クラス宣言

以下は com.google.gwt.user.client.ui.FormPanel クラスの宣言です-

public class FormPanel
   extends SimplePanel
      implements FiresFormEvents,
         com.google.gwt.user.client.ui.impl.FormPanelImplHost

クラスコンストラクター

Sr.No. Constructor & Description
1

FormPanel()

新しいFormPanelを作成します。

2

protected FormPanel(Element element)

このコンストラクタは、既存の要素を明示的に使用するためにサブクラスによって使用される場合があります。

3

protected FormPanel(Element element, boolean createIFrame)

このコンストラクタは、既存の要素を明示的に使用するためにサブクラスによって使用される場合があります。

4

FormPanel(NamedFrame frameTarget)

NamedFrameをターゲットとするFormPanelを作成します。

5

FormPanel(java.lang.String target)

新しいFormPanelを作成します。

クラスメソッド

Sr.No. Function name & Description
1

void add Form Handler (FormHandler handler)

廃止予定です。 代わりに、送信完了ハンドラー(com.google.gwt.user.client.ui.Form Panel.Submit Complete Handler)を追加し、送信ハンドラー(com.google.gwt.user.client.ui.Form Panel.Submit Handler)を追加します。

2

Handler Registration addSubmit Complete Handler (FormPanel.SubmitCompleteHandler handler)

FormPanel.Submit Complete Eventハンドラーを追加します。

3

HandlerRegistration addSubmitHandler(FormPanel.SubmitHandler handler)

FormPanel.SubmitEventハンドラーを追加します。

4

java.lang.String getAction()

このフォームに関連付けられた「アクション」を取得します。

5

java.lang.String getEncoding()

このフォームの送信に使用されるエンコードを取得します。

6

java.lang.String getMethod()

このフォームの送信に使用されるHTTPメソッドを取得します。

7

java.lang.String getTarget()

フォームの「ターゲット」を取得します。

8

protected void onAttach()

このメソッドは、ウィジェットがブラウザのドキュメントに添付されたときに呼び出されます。

9

protected void onDetach()

このメソッドは、ウィジェットがブラウザのドキュメントから切り離されるときに呼び出されます。

10

boolean onFormSubmit()

フォームが送信されると発生します。

11 *void onFrameLoad() *
12
  • void removeFormHandler(FormHandler handler)*

廃止予定です。 によって返されるオブジェクトでHandlerRegistration.removeHandler()メソッドを使用し、代わりにadd *Handlerメソッドを使用します

13
  • void reset()*

フォームをリセットして、すべてのフィールドをクリアします。

14

void setAction(java.lang.String url)

このフォームに関連付けられた「アクション」を設定します。

15

void setEncoding(java.lang.String encodingType)

このフォームの送信に使用されるエンコードを設定します。

16

void setMethod(java.lang.String method)

このフォームの送信に使用されるHTTPメソッドを設定します。

17

void submit()

フォームを送信します。

18

static FormPanel wrap(Element element)

既存の<form>要素をラップするFormPanelを作成します。

19

static FormPanel wrap(Element element, boolean createIFrame)

既存の<form>要素をラップするFormPanelを作成します。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • 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

FormPanelウィジェットの例

この例では、GWTでFormPanelウィジェットの使用方法を示す簡単な手順を紹介します。 次の手順に従って、_GWTで作成したGWTアプリケーションを更新します-アプリケーションの作成_の章-

Step Description
1 Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the application to verify the result of the implemented logic.

以下は、変更されたモジュール記述子 src/com.finddevguides/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.finddevguides.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>

以下は、変更されたスタイルシートファイル 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;
}

以下は、変更されたHTMLホストファイル war/HelloWorldl の内容です。

<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>

FormPanelウィジェットの使用法を示すJavaファイル src/com.finddevguides/HelloWorld.java の内容を見てみましょう。

package com.finddevguides.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);
   }

}

すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-

GWT FormPanelウィジェット