tableview in javafx

w‮figi.ww‬tidea.com

In JavaFX, a TableView is a control that displays a set of data in a tabular form. It allows users to interact with the data by selecting, sorting, filtering, and editing rows.

Here's an example of how to create and use a TableView in a JavaFX application:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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;

public class MyJavaFXApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Create a list of data to display in the table
        ObservableList<Person> data = FXCollections.observableArrayList(
            new Person("John", "Doe", 30),
            new Person("Jane", "Doe", 25),
            new Person("Bob", "Smith", 40)
        );
        
        // Create columns for the table
        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"));
        
        // Create a new TableView and set the data and columns
        TableView<Person> tableView = new TableView<>(data);
        tableView.getColumns().addAll(firstNameCol, lastNameCol, ageCol);
        
        // Add the TableView to a VBox layout
        VBox layout = new VBox(tableView);
        
        // Create a Scene object with the layout and set it on the stage
        Scene scene = new Scene(layout, 300, 200);
        primaryStage.setScene(scene);

        // Show the stage
        primaryStage.show();
    }

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

class Person {
    private String firstName;
    private String lastName;
    private 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;
    }
}

In this example, we create a TableView with a set of Person objects. We define three columns for the table, one for each field in the Person class. We then add the columns to the TableView and set the data using the setItems() method. We add the TableView to a VBox layout and display the layout on the stage.

The TableView class provides a number of methods for configuring the appearance and behavior of the control. You can add and remove columns using the getColumns() method, set the data using the setItems() method, and customize the appearance of each cell in the table using the setCellFactory() method. You can also use the setEditable() method to enable editing of the data in the table. Additionally, the TableView class provides methods for sorting and filtering the data in the table.