TableColumn
1️⃣ What is TableColumn?
TableColumnrepresents a single column in aTableView.- Each column is responsible for displaying one property of the items in the table.
- Usually, you pair it with a data model class (like
Person,Product, etc.). - Columns can be editable or sortable.
2️⃣ Key Features
setCellValueFactory()→ defines which property of the data model to show in the column.- Can set custom cell rendering using
setCellFactory(). - Can have multiple columns in a single
TableView.
3️⃣ Example with Java Code
Suppose we have a Person class:
public class Person {
private final String firstName;
private final String lastName;
private final int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public int getAge() { return age; }
}
Then a TableView with TableColumns:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
public class TableColumnExample extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Person> tableView = new TableView<>();
// Create columns
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn<Person, Integer> ageCol = new TableColumn<>("Age");
ageCol.setCellValueFactory(new PropertyValueFactory<>("age"));
tableView.getColumns().addAll(firstNameCol, lastNameCol, ageCol);
// Add sample data
tableView.setItems(FXCollections.observableArrayList(
new Person("Ahmed", "Khaldi", 26),
new Person("Sara", "Benali", 22),
new Person("Omar", "Fares", 30)
));
VBox root = new VBox(tableView);
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("TableColumn Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
✅ Explanation:
- Each
TableColumnusesPropertyValueFactoryto get the value fromPerson. - The generic
<Person, String>means this column is for Person, and its cell values are String. - You can add as many columns as you want.
4️⃣ FXML Version
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx/17.0.2" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.TableColumnController"
spacing="10" style="-fx-padding: 20;">
<TableView fx:id="tableView">
<columns>
<TableColumn fx:id="firstNameCol" text="First Name"/>
<TableColumn fx:id="lastNameCol" text="Last Name"/>
<TableColumn fx:id="ageCol" text="Age"/>
</columns>
</TableView>
</VBox>
Controller
package com.example;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class TableColumnController {
@FXML
private TableView<Person> tableView;
@FXML
private TableColumn<Person, String> firstNameCol;
@FXML
private TableColumn<Person, String> lastNameCol;
@FXML
private TableColumn<Person, Integer> ageCol;
@FXML
public void initialize() {
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
ageCol.setCellValueFactory(new PropertyValueFactory<>("age"));
tableView.setItems(FXCollections.observableArrayList(
new Person("Ahmed", "Khaldi", 26),
new Person("Sara", "Benali", 22),
new Person("Omar", "Fares", 30)
));
}
}