JavaFX table with multi-line, background color and centered text

In JavaView TableView how can I

  • Create a multi-line column?
  • Center its content?
  • And set a background color for each (solid) line?

I was able to create a multi-line column using a custom one CellFactory

. I also know setAlignment(Pos.CENTER)

and setTextAlignment(TextAlignment.CENTER)

for the center of the text. However, the text in my sample app is not centered correctly on the line. Also, I was unable to set the background color for objects Text

. Now my approach is to add Pane

for each line, which works fine. But how do I make it Pane

fill the entire column width and 1/3 of its height?

As a starting point, here's how I might expect the code (although, I know it doesn't do 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 expect an output like this: JavaFX TableView with multiline column

For a complete, compiled code example, please check out this pastebin .

+3


source to share


1 answer


Use a suitable layout panel (for example VBox

) and add to it Label

. You can customize the labels to fill the width VBox

using VBox.setHgrow(...)

. You also need to set a maximum width for the label so it can grow.

As an aside, it's not a good practice to re-create controls every time a method is called updateItem(...)

. Create them once and then just configure them in the method updateItem(...)

with the required data.



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);
            }
        }
    });

      

+5


source







All Articles