Jacobian
Jacobian

Reputation: 10812

colspan and rowspan for tableview in JavaFX

I could not find any examples on the Web that show how to make hierarchical headers for JavaFX TableView, therefore, I do not provide any code. I will just post an image of what I want to achieve. Here it is:

enter image description here

It is highly required in many application to have such table grids. Unfortunately, I could not find any example for JavaFX. Thanks for help in advance!

Upvotes: 1

Views: 4917

Answers (1)

Prometheus
Prometheus

Reputation: 1015

It seems you are looking for nested columns.

You can add sub-columns to a main column to get hierarchical headers. Here is a small example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class NestedColumns extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        TableView<String> tableView = new TableView<String>();
        TableColumn<String, String> nameColumn = new TableColumn<>("Name");
        TableColumn<String, String> firstNameColumn = new TableColumn<>("First name");
        TableColumn<String, String> lastNameColumn = new TableColumn<>("Last name");
        nameColumn.getColumns().addAll(firstNameColumn, lastNameColumn);

        tableView.getColumns().add(nameColumn);

        Scene scene = new Scene(tableView);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 3

Related Questions