Sunday 17 June 2018

JavaFX: Create table

By using 'TableView' class, you can create a table.

Ex
TableView tableView = new TableView();

How to create a column?
By using 'TableColumn' class, you can create column widget.

Ex
TableColumn firstNameCol = new TableColumn("First Name");

How to add columns to a table?
You can add columns to a table view.

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

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

		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