Gwt-textarea-widget

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

GWT-TextAreaウィジェット

前書き

*TextArea* ウィジェットは、複数行のテキストを入力できるテキストボックスを表します。

クラス宣言

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

public class TextArea
   extends TextBoxBase
      implements HasDirection

CSSスタイルルール

次のデフォルトのCSSスタイルルールがすべてのTextBoxウィジェットに適用されます。 要件に応じて上書きできます。

.gwt-TextArea {}

.gwt-TextArea-readonly {}

クラスコンストラクター

Sr.No. Constructor & Description
1

TextArea()

空のテキスト領域を作成します。

クラスメソッド

Sr.No. Function name & Description
1

int getCharacterWidth()

テキストボックスの要求された幅を取得します(すべての文字が同じように作成されるわけではないため、これは正確な値ではありません)。

2

int getCursorPos()

カーソルの現在の位置を取得します(これはテキスト選択の開始としても機能します)。

3

HasDirection.Direction getDirection()

ウィジェットの方向性を取得します。

4

int getSelectionLength()

現在のテキスト選択の長さを取得します。

5

int getVisibleLines()

表示されているテキスト行の数を取得します。

6

void setCharacterWidth(int width)

要求されたテキストボックスの幅を設定します(すべての文字が同じように作成されるわけではないため、これは正確な値ではありません)。

7

void setDirection(HasDirection.Direction direction)

ウィジェットの方向性を設定します。

8

void setVisibleLines(int lines)

表示されるテキスト行の数を設定します。

継承されるメソッド

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

  • com.google.gwt.user.client.ui.UIObject
  • com.google.gwt.user.client.ui.Widget
  • com.google.gwt.user.client.ui.FocusWidget
  • com.google.gwt.user.client.ui.TextBoxBase
  • java.lang.Object

TextBoxウィジェットの例

この例では、GWTでTextBoxウィジェットの使用方法を示す簡単な手順を紹介します。 次の手順に従って、_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;
}

.gwt-TextArea {
   color: green;
}

.gwt-TextArea-readonly {
   background-color: yellow;
}

以下は、変更された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>TextArea Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

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

package com.finddevguides.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
     //create textarea elements
      TextArea textArea1 = new TextArea();
      TextArea textArea2 = new TextArea();

     //set width as 10 characters
      textArea1.setCharacterWidth(20);
      textArea2.setCharacterWidth(20);

     //set height as 5 lines
      textArea1.setVisibleLines(5);
      textArea2.setVisibleLines(5);

     //add text to text area
      textArea2.setText(" Hello World! \n Be Happy! \n Stay Cool!");

     //set textbox as readonly
      textArea2.setReadOnly(true);

     //Add text boxes to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(textArea1);
      panel.add(textArea2);

      RootPanel.get("gwtContainer").add(panel);
   }
}

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

GWT TextAreaウィジェット