Tables

The most direct way to organize widgets in a tabular grid is using WTable. This widget renders as an HTML <table> element.

Each table cell is a Container to which contents can be added. The table will grow as necessary while you add data to it.

Our first example shows a plain table, with default browser styling.

Example
#First NameLast NamePay
1MarkOtto
2JacobThornton
3Larry the Bird
source
  class Employee {
    private static Logger logger = LoggerFactory.getLogger(Employee.class);

    public String firstName;
    public String lastName;
    public double pay;

    Employee(final String aFirstName, final String aLastName, double aPay) {
      this.firstName = aFirstName;
      this.lastName = aLastName;
      this.pay = aPay;
    }
  }

  public Employee[] employees = {
    new Employee("Mark", "Otto", 100),
    new Employee("Jacob", "Thornton", 50),
    new Employee("Larry the Bird", "", 10)
  };

  void PlainTable() {
    WTable table = new WTable();
    table.setHeaderCount(1);
    table.setWidth(new WLength("100%"));
    new WText("#", (WContainerWidget) table.getElementAt(0, 0));
    new WText("First Name", (WContainerWidget) table.getElementAt(0, 1));
    new WText("Last Name", (WContainerWidget) table.getElementAt(0, 2));
    new WText("Pay", (WContainerWidget) table.getElementAt(0, 3));
    for (int i = 0; i < 3; ++i) {
      final Employee employee = employees[i];
      int row = i + 1;
      new WText(new WString("{1}").arg(row), (WContainerWidget) table.getElementAt(row, 0));
      new WText(employee.firstName, (WContainerWidget) table.getElementAt(row, 1));
      new WText(employee.lastName, (WContainerWidget) table.getElementAt(row, 2));
      new WLineEdit(
          new WString("{1}").arg(employee.pay).toString(),
          (WContainerWidget) table.getElementAt(row, 3));
    }
  }

If you only need to display a static table (with a fixed number of rows and columns), then you might just as well consider using a WTemplate containing the HTML markup for a table, and providing the contents (widgets or strings) by substituting placeholders.

In contrast, the WTable class is suitable when the table is to be constructed dynamically, based on information that may be variable in size.

Finally, you may want to consider using a WTableView if you would like to display large amounts of data (more than could fit in memory!), or if you would like the user to be able to resort the data or resize columns.

Top

Bootstrap styling

The bootstrap theme provides optional markup for the table. These styles are enabled by adding the "table" style class, and other optional style classes. The other styling options are enabled by adding one of the following classes:

table-bordered
Adds borders
table-hover
Enables a row hover effect
table-sm
Makes the table more compact
table-striped
Adds alternating row colors

Example
#First NameLast NamePay
1MarkOtto
2JacobThornton
3Larry the Bird
Options:
source
  class Employee {
    private static Logger logger = LoggerFactory.getLogger(Employee.class);

    public String firstName;
    public String lastName;
    public double pay;

    Employee(final String aFirstName, final String aLastName, double aPay) {
      this.firstName = aFirstName;
      this.lastName = aLastName;
      this.pay = aPay;
    }
  }

  public Employee[] employees = {
    new Employee("Mark", "Otto", 100),
    new Employee("Jacob", "Thornton", 50),
    new Employee("Larry the Bird", "", 10)
  };

  void addOptionToggle(
      final WWidget widget, String option, final String styleClass, WContainerWidget parent) {
    final WCheckBox checkBox = new WCheckBox(option, (WContainerWidget) parent);
    checkBox.setInline(false);
    checkBox
        .changed()
        .addListener(
            this,
            () -> {
              widget.toggleStyleClass(styleClass, checkBox.isChecked());
            });
  }

  void StyledTable() {
    WTable table = new WTable();
    WTable table_ = table;
    table_.setHeaderCount(1);
    new WText("#", (WContainerWidget) table_.getElementAt(0, 0));
    new WText("First Name", (WContainerWidget) table_.getElementAt(0, 1));
    new WText("Last Name", (WContainerWidget) table_.getElementAt(0, 2));
    new WText("Pay", (WContainerWidget) table_.getElementAt(0, 3));
    for (int i = 0; i < 3; ++i) {
      final Employee employee = employees[i];
      int row = i + 1;
      new WText(new WString("{1}").arg(row), (WContainerWidget) table_.getElementAt(row, 0));
      new WText(employee.firstName, (WContainerWidget) table_.getElementAt(row, 1));
      new WText(employee.lastName, (WContainerWidget) table_.getElementAt(row, 2));
      new WLineEdit(
          new WString("{1}").arg(employee.pay).toString(),
          (WContainerWidget) table_.getElementAt(row, 3));
    }
    table_.addStyleClass("table");
    WContainerWidget result = new WContainerWidget();
    result.addWidget(table);
    new WText("Options:", (WContainerWidget) result);
    addOptionToggle(table_, "borders", "table-bordered", result);
    addOptionToggle(table_, "hover", "table-hover", result);
    addOptionToggle(table_, "small", "table-sm", result);
    addOptionToggle(table_, "stripes", "table-striped", result);
  }

Top