简体   繁体   English

具有多行,背景颜色和居中文本的JavaFX表

[英]JavaFX table with multiline, background color and centered text

In a JavaFX TableView, how can I 在JavaFX TableView中,我该怎么做

  • Create a multiline column? 创建多行列?
  • Center its content? 中心内容?
  • And set background color for each (entire) line? 并为每条(整条)线设置背景颜色?

I managed to create a multiline column using a custom CellFactory . 我设法使用自定义CellFactory创建一个多CellFactory I'm also aware of setAlignment(Pos.CENTER) and setTextAlignment(TextAlignment.CENTER) to center text. 我也知道setAlignment(Pos.CENTER)setTextAlignment(TextAlignment.CENTER)到中心文本。 However, the text in my sample app is not centered properly per line. 但是,我的示例应用程序中的文本没有按行正确居中。 Furthermore, I did not manage to set a background color on the Text objects. 此外,我没有设法在Text对象上设置背景颜色。 Now my approach is to add a Pane for each line, which works fine. 现在我的方法是为每一行添加一个Pane ,这很好。 But how do I make the Pane fill the column's entire width and 1/3rd of its height? 但是如何让Pane填满柱子的整个宽度和高度的1/3呢?

As a starting point, this is how I would expect the code to be (though, I'm aware it's not doing what I want): 作为一个起点,这就是我期望代码的方式(但是,我知道它不是我想做的):

    multiCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
      @Override public TableCell<Person, Person> call(TableColumn<Person, Person> multiCol) {
        return new TableCell<Person, Person>() {
           private Group grp = null;

           @Override public void updateItem(final Person person, boolean empty) {
              super.updateItem(person, empty);

              this.setAlignment(Pos.CENTER);

              if (!isEmpty()) {
                 Text text = new Text(person.getFirstName());
                 text.setX(0);
                 text.setY(0);
                 text.setTextAlignment(TextAlignment.CENTER); // Center text?

                 Pane pane = new Pane();
                 pane.setStyle("-fx-background-color: #66BB66;");
                 pane.setLayoutX(0);
                 pane.setLayoutY(0);
                 pane.setPrefHeight(20);
                 pane.setPrefWidth(this.prefWidth(-1)); // Column width?

                 // -----

                 Text text2 = new Text(person.getLastName());
                 text2.setX(0);
                 text2.setY(20);
                 text2.setTextAlignment(TextAlignment.CENTER); // Center text?

                 Pane pane2 = new Pane();
                 pane2.setStyle("-fx-background-color: #79A8D8;");
                 pane2.setLayoutX(0);
                 pane2.setLayoutY(20);
                 pane2.setPrefHeight(20);
                 pane2.setPrefWidth(this.prefWidth(-1)); // Column width?

                 // -----

                 Text text3 = new Text(person.getEmail());
                 text3.setX(0);
                 text3.setY(40);
                 text3.setTextAlignment(TextAlignment.CENTER); // Center text?

                 Pane pane3 = new Pane();
                 pane3.setStyle("-fx-background-color: #FF8888;");
                 pane3.setLayoutX(0);
                 pane3.setLayoutY(40);
                 pane3.setPrefHeight(20);
                 pane3.setPrefWidth(this.prefWidth(-1)); // Column width?

                 // -----

                 Group grp = new Group();

                 grp.getChildren().add(pane);
                 grp.getChildren().add(text);

                 grp.getChildren().add(pane2);
                 grp.getChildren().add(text2);

                 grp.getChildren().add(pane3);
                 grp.getChildren().add(text3);

                 setGraphic(grp);

                 setStyle("-fx-padding: 0 0 0 0;");
              }
            }
          };
        }
      });

I'm expecting an output like this: 我期待这样的输出: 带有多行列的JavaFX TableView

For a full, compilable code sample please check out this pastebin . 有关完整的可编译代码示例,请查看此pastebin

Use a suitable layout pane (eg a VBox ), and add Label s to it. 使用合适的布局窗格(例如VBox ),并将Label添加到其中。 You can configure the labels to fill the width of a VBox using VBox.setHgrow(...) . 您可以使用VBox.setHgrow(...)配置标签以填充VBox的宽度。 You also need to set the maximum width of the label to allow it to grow. 您还需要设置标签的最大宽度以允许其增长。

As an aside, it is not good practice to re-create the controls every time the updateItem(...) method is called. updateItem(...)每次updateItem(...)方法时重新创建控件都不是好习惯。 Create them once and then just configure them in the updateItem(...) method with the required data. 创建它们一次,然后在updateItem(...)方法中使用所需数据配置它们。

Example: 例:

    TableColumn<Person, Person> multiCol = new TableColumn<>("Multiline");
    multiCol.setCellValueFactory(cellData -> 
        new ReadOnlyObjectWrapper<Person>(cellData.getValue()));
    multiCol.setCellFactory(column -> new TableCell<Person, Person>() {

        private VBox graphic ;
        private Label firstNameLabel ;
        private Label lastNameLabel ;
        private Label emailLabel ;

        // Anonymous constructor:
        {
            graphic = new VBox();
            firstNameLabel = createLabel("#66BB66");
            lastNameLabel = createLabel("#79A8D8");
            emailLabel = createLabel("#FF8888");
            graphic.getChildren().addAll(firstNameLabel, 
                    lastNameLabel, emailLabel);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        private final Label createLabel(String color) {
            Label label = new Label();
            VBox.setVgrow(label, Priority.ALWAYS);
            label.setMaxWidth(Double.MAX_VALUE);
            label.setStyle("-fx-background-color: "+color+" ;");
            label.setAlignment(Pos.CENTER);
            return label ;
        }

        @Override
        public void updateItem(Person person, boolean empty) {
            if (person == null) {
                setGraphic(null);
            } else {
                firstNameLabel.setText(person.getFirstName());
                lastNameLabel.setText(person.getLastName());
                emailLabel.setText(person.getEmail());
                setGraphic(graphic);
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM