带有多行表头的JavaFX 2.0 Table [英] JavaFX 2.0 Table with multiline table header

查看:145
本文介绍了带有多行表头的JavaFX 2.0 Table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使JavaFX 2.0表带有多行标题? 我在网上找到的所有示例都有表格,其中columnt标头宽度=文字大小,不包含换行符.屏幕上显示我所拥有的和所需要的一切:

Is it possible to make javaFX 2.0 table with multiline header? All the examples, which i have found on the web, have tables, where columnt header width = its text size, without wraping. The exaple of what i have, and what i need is shown on a screen:

推荐答案

我想出了以下功能:

private void makeHeaderWrappable(TableColumn col) {
  Label label = new Label(col.getText());
  label.setStyle("-fx-padding: 8px;");
  label.setWrapText(true);
  label.setAlignment(Pos.CENTER);
  label.setTextAlignment(TextAlignment.CENTER);

  StackPane stack = new StackPane();
  stack.getChildren().add(label);
  stack.prefWidthProperty().bind(col.widthProperty().subtract(5));
  label.prefWidthProperty().bind(stack.prefWidthProperty());
  col.setGraphic(stack);
}

完整的可执行示例是此要点中的 (需要

A complete executable example is in this gist (requires the 2.2 developer preview as a minimum).

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class TableWrappedHeaders extends Application {
  public static void main(String[] args) { launch(args); }

  @Override public void start(Stage stage) {
    TableColumn firstNameCol = new TableColumn("First Name (which is a really long name)");
    makeHeaderWrappable(firstNameCol);
    firstNameCol.setPrefWidth(100);
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setPrefWidth(100);
    lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));

    TableView table = new TableView();
    table.getColumns().addAll(firstNameCol, lastNameCol);
    table.setItems(FXCollections.observableArrayList(
      new Person("Jacob", "Smith"),
      new Person("Isabella", "Johnson"),
      new Person("Ethan", "Williams")
    ));
    table.setPrefSize(250, 200);

    Pane layout = new VBox(10);
    layout.setStyle("-fx-padding: 10;");
    layout.getChildren().addAll(table);
    stage.setScene(new Scene(layout));
    stage.show();
  }

  private void makeHeaderWrappable(TableColumn col) {
    Label label = new Label(col.getText());
    label.setStyle("-fx-padding: 8px;");
    label.setWrapText(true);
    label.setAlignment(Pos.CENTER);
    label.setTextAlignment(TextAlignment.CENTER);

    StackPane stack = new StackPane();
    stack.getChildren().add(label);
    stack.prefWidthProperty().bind(col.widthProperty().subtract(5));
    label.prefWidthProperty().bind(stack.prefWidthProperty());
    col.setText(null);
    col.setGraphic(stack);
  }

  public static class Person {
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;

    private Person(String fName, String lName) {
      this.firstName = new SimpleStringProperty(fName);
      this.lastName = new SimpleStringProperty(lName);
    }

    public String getFirstName() { return firstName.get(); }
    public void setFirstName(String fName) { firstName.set(fName); }
    public String getLastName() { return lastName.get(); }
    public void setLastName(String fName) { lastName.set(fName); }
  }
}

可能有更好的方法来执行此操作,但是上面的函数至少对我的测试用例有用.

There is probably a better way to do this, but the function above did at least work for me in my test case.

我认为这可以通过简单的CSS样式实现,但是我无法仅通过CSS来实现.

I thought this would be achievable with a simple css style, but I could not get it to work via css alone.

这篇关于带有多行表头的JavaFX 2.0 Table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆