How to add data to a TableView in JavaFX?


TableView is a component that is used to create a table populate it, and remove items from it. You can create a table view by instantiating thejavafx.scene.control.TableView class.

Example

The following Example demonstrates how to create a TableView and add data to it.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class SettingData extends Application {
   public void start(Stage stage) {
      //Label for education
      Label label = new Label("File Data:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a table view
      TableView<FileData> table = new TableView<FileData>();
      final ObservableList<FileData> data = FXCollections.observableArrayList(
         new FileData("file1", "D:\myFiles\file1.txt", "25 MB", "12/01/2017"),
         new FileData("file2", "D:\myFiles\file2.txt", "30 MB", "01/11/2019"),
         new FileData("file3", "D:\myFiles\file3.txt", "50 MB", "12/04/2017"),
         new FileData("file4", "D:\myFiles\file4.txt", "75 MB", "25/09/2018")
      );
      //Creating columns
      TableColumn fileNameCol = new TableColumn("File Name");
      fileNameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
      TableColumn pathCol = new TableColumn("Path");
      pathCol.setCellValueFactory(new PropertyValueFactory("path"));
      TableColumn sizeCol = new TableColumn("Size");
      sizeCol.setCellValueFactory(new PropertyValueFactory("size"));
      TableColumn dateCol = new TableColumn("Date Modified");
      dateCol.setCellValueFactory(new PropertyValueFactory("dateModified"));
      dateCol.setPrefWidth(100);
      //Adding data to the table
      ObservableList<String> list = FXCollections.observableArrayList();
      table.setItems(data);
      table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
      table.getColumns().addAll(fileNameCol, pathCol, sizeCol, dateCol);
      //Setting the size of the table
      table.setMaxSize(350, 200);
      VBox vbox = new VBox();
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(10, 50, 50, 60));
      vbox.getChildren().addAll(label, table);
      //Setting the scene
      Scene scene = new Scene(vbox, 595, 230);
      stage.setTitle("Table View Exmple");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

FileData class −

import javafx.beans.property.SimpleStringProperty;
public class FileData {
   SimpleStringProperty fileName;
   SimpleStringProperty path;
   SimpleStringProperty size;
   SimpleStringProperty dateModified;
   FileData(String fileName, String path, String size, String dateModified) {
      this.fileName = new SimpleStringProperty(fileName);
      this.path = new SimpleStringProperty(path);
      this.size = new SimpleStringProperty(size);
      this.dateModified = new SimpleStringProperty(dateModified);
   }
   public String getFileName(){
      return fileName.get();
   }
   public void setFileName(String fname){
      fileName.set(fname);
   }
   public String getPath(){
      return path.get();
   }
   public void setPath(String fpath){
      path.set(fpath);
   }
   public String getSize(){
      return size.get();
   }
   public void setSize(String fsize){
      size.set(fsize);
   }
   public String getDateModified(){
      return dateModified.get();
   }
   public void setModified(String fmodified){
      dateModified.set(fmodified);
   }
}

Output

Updated on: 18-May-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements