GWT - FlexTable 组件



介绍

FlexTable 组件表示一个灵活的表格,它可以按需创建单元格。它可以是不规则的(即每一行可以包含不同数量的单元格),并且可以将单个单元格设置为跨越多行或多列。

类声明

以下是com.google.gwt.user.client.ui.FlowPanel类的声明:

public class FlexTable
   extends HTMLTable

类构造函数

序号 构造函数 & 描述
1

FlexTable()

空Flex表格的构造函数。

类方法

序号 函数名称 & 描述
1

void addCell(int row)

将单元格添加到指定行。

2

int getCellCount(int row)

获取给定行上的单元格数量。

3

FlexTable.FlexCellFormatter getFlexCellFormatter()

显式获取 FlexTable.FlexCellFormatter。

4

int getRowCount()

获取行数。

5

void insertCell(int beforeRow, int beforeColumn)

将单元格插入到 FlexTable 中。

6

int insertRow(int beforeRow)

将一行插入到 FlexTable 中。

7

protected void prepareCell(int row, int column)

确保单元格存在。

8

protected void prepareRow(int row)

确保行存在。

9

void removeAllRows()

移除表格中的所有行。

10

void removeCell(int row, int col)

从表格中移除指定的单元格。

11

void removeCells(int row, int column, int num)

从表格中移除指定行中的多个单元格。

12

void removeRow(int row)

从表格中移除指定行。

继承的方法

此类继承自以下类的方法:

  • 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.HTMLTable

  • java.lang.Object

FlexTable 组件示例

此示例将引导您完成简单的步骤,以演示如何在 GWT 中使用 FlexTable 组件。按照以下步骤更新我们在GWT - 创建应用章节中创建的 GWT 应用:

步骤 描述
1 按照GWT - 创建应用章节中的说明,创建一个名为HelloWorld的项目,放在com.tutorialspoint包下。
2 修改HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java,如下所述。其余文件保持不变。
3 编译并运行应用程序以验证已实现逻辑的结果。

以下是修改后的模块描述符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>

以下是修改后的样式表文件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;
}
.flexTable td {
   border: 1px solid #BBBBBB;
   padding: 3px;
}

.flexTable-buttonPanel td {
   border: 0px;
}

.fixedWidthButton {
   width: 150px;
}

以下是修改后的 HTML 宿主文件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>FlexTable Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

让我们来看一下 Java 文件src/com.tutorialspoint/HelloWorld.java的内容,它将演示 FlexTable 组件的使用。

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.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {

   public void onModuleLoad() {
      // Create a Flex Table
      final FlexTable flexTable = new FlexTable();
      FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
      flexTable.addStyleName("flexTable");
      flexTable.setWidth("32em");
      flexTable.setCellSpacing(5);
      flexTable.setCellPadding(3);

      // Add some text
      cellFormatter.setHorizontalAlignment(
      0, 1, HasHorizontalAlignment.ALIGN_LEFT);
      flexTable.setHTML(0, 0, "This is a FlexTable that supports"
         +" <b>colspans</b> and <b>rowspans</b>."
         +" You can use it to format your page"
         +" or as a special purpose table.");
      cellFormatter.setColSpan(0, 0, 2);

      // Add a button that will add more rows to the table
      Button addRowButton = new Button("Add a Row"); 
      addRowButton.addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
            addRow(flexTable);
         }
      });

      addRowButton.addStyleName("fixedWidthButton");

      // Add a button that will add more rows to the table
      Button removeRowButton = new Button("Remove a Row"); 
      removeRowButton.addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
            removeRow(flexTable);
         }
      });

      removeRowButton.addStyleName("fixedWidthButton");

      VerticalPanel buttonPanel = new VerticalPanel();
      buttonPanel.setStyleName("flexTable-buttonPanel");
      buttonPanel.add(addRowButton);
      buttonPanel.add(removeRowButton);
      flexTable.setWidget(0, 1, buttonPanel);
      cellFormatter.setVerticalAlignment(0, 1, 
      HasVerticalAlignment.ALIGN_TOP);

      // Add two rows to start
      addRow(flexTable);
      addRow(flexTable);

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

   /**
    * Add a row to the flex table.
    */
   private void addRow(FlexTable flexTable) {
      int numRows = flexTable.getRowCount();
      flexTable.setWidget(numRows, 0, 
      new Image("https://tutorialspoint.com/images/gwt-mini.png"));
      flexTable.setWidget(numRows, 1, 
      new Image("https://tutorialspoint.com/images/gwt-mini.png"));
      flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
   }

   /**
    * Remove a row from the flex table.
    */
   private void removeRow(FlexTable flexTable) {
      int numRows = flexTable.getRowCount();
      if (numRows > 1) {
         flexTable.removeRow(numRows - 1);
         flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows - 1);
      }
   }
}

完成所有更改后,让我们像在GWT - 创建应用章节中一样,在开发模式下编译并运行应用程序。如果应用程序一切正常,则会产生以下结果:

GWT FlexTable Widget
gwt_layout_panels.htm
广告
© . All rights reserved.