Sunday, 17 June 2018

JavaFX: Add nested columns to a table

By adding columns to a column widget, you can create nested columns.

Ex
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(300);

TableColumn primaryEmail = new TableColumn("primaryEmail");
primaryEmail.setMinWidth(130);

TableColumn secondaryEmail = new TableColumn("secondaryEmail");
secondaryEmail.setMinWidth(130);


emailCol.getColumns().addAll(primaryEmail, secondaryEmail);

Find the below working application.

TableViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewApp extends Application {

	@Override
	public void start(Stage primaryStage) {
		Label label = new Label("My Friends List");
		label.setFont(new Font("Arial", 30));

		TableView tableView = new TableView();

		tableView.setEditable(true);
		TableColumn firstNameCol = new TableColumn("First Name");
		firstNameCol.setMinWidth(300);

		TableColumn lastNameCol = new TableColumn("Last Name");
		lastNameCol.setMinWidth(300);

		TableColumn emailCol = new TableColumn("Email");
		emailCol.setMinWidth(300);

		TableColumn primaryEmail = new TableColumn("primaryEmail");
		primaryEmail.setMinWidth(130);

		TableColumn secondaryEmail = new TableColumn("secondaryEmail");
		secondaryEmail.setMinWidth(130);

		emailCol.getColumns().addAll(primaryEmail, secondaryEmail);

		tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

		VBox vBox = new VBox(10, label, tableView);
		vBox.setSpacing(5);
		vBox.setPadding(new Insets(10, 10, 10, 10));

		primaryStage.setScene(new Scene(vBox));
		primaryStage.setTitle("Table View Example");
		primaryStage.setWidth(900);
		primaryStage.setHeight(500);
		primaryStage.show();
	}

}

TestFX.java

package com.sample.demos;

import javafx.application.Application;

public class TestFX {
	public static void main(String args[]) {
		Application.launch(TableViewApp.class, args);
	}
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment