Gwt-htmlpanel-widget

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

GWT-HTMLPanelウィジェット

前書き

*HTMLPanel* ウィジェットは、HTMLを含むパネルを表し、子ウィジェットをそのHTML内の識別された要素にアタッチできます。

クラス宣言

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

public class HTMLPanel
   extends ComplexPanel

クラスコンストラクター

Sr.No. Constructor & Description
1

HTMLPanel(SafeHtml safeHtml)

指定されたSafeHtmlオブジェクトからパネルのHTMLを初期化します。

2

HTMLPanel(java.lang.String html)

DIV要素内に指定されたHTMLコンテンツを含むHTMLパネルを作成します。

3

HTMLPanel(java.lang.String tag, java.lang.String html)

指定されたタグをルート要素に持つHTMLパネルを、指定されたHTMLコンテンツで作成します。

クラスメソッド

Sr.No. Function name & Description
1

void add(Widget widget, Element elem)

HTML要素内に含まれる子ウィジェットをパネルに追加します。

2

void add(Widget widget, java.lang.String id)

指定されたIDで指定されたHTML要素内に含まれる子ウィジェットをパネルに追加します。

3

void addAndReplaceElement(Widget widget, Element toReplace)

子ウィジェットをパネルに追加して、HTML要素を置き換えます。

4

void addAndReplaceElement(Widget widget, java.lang.String id)

指定されたIDで指定されたHTML要素を置き換えて、子ウィジェットをパネルに追加します。

5

static java.lang.String createUniqueId()

動的に生成されたHTML内の要素の一意のIDを作成するためのヘルパーメソッド。

6

Element getElementById(java.lang.String id)

IDでこのパネル内の要素を検索します。

継承されるメソッド

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

  • 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.ComplexPanel
  • java.lang.Object

HTMLPanelウィジェットの例

この例では、GWTでHTMLPanelウィジェットの使用方法を示す簡単な手順を紹介します。 次の手順に従って、_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>HTMLPanel Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

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

package com.finddevguides.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      String htmlString = "This is a <b>HTMLPanel</b> containing"
         +" html contents. "
         +" <i>By putting some fairly large contents in the middle"
         +" and setting its size explicitly, it becomes a scrollable area"
         +" within the page, but without requiring the use of an IFRAME.</i>"
         +" <u>Here's quite a bit more meaningless text that will serve"
         +" to make this thing scroll off the bottom of its visible area."
         +" Otherwise, you might have to make it really, really"
         +" small in order to see the nifty scroll bars!</u>";

      HTMLPanel htmlPanel = new HTMLPanel(htmlString);

      DecoratorPanel panel = new DecoratorPanel();
      panel.add(htmlPanel);

     //Add the widgets to the root panel.
      RootPanel.get().add(panel);
   }
}

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

GWT HTMLPanelウィジェット