如何在 JavaFX 中向 TableView 添加数据?

javafxobject oriented programmingprogramming更新于 2025/4/14 9:52:17

TableView 是一个用于创建表格、填充表格并从中删除项目的组件。您可以通过实例化 javafx.scene.control.TableView 类来创建表格视图。

示例

以下示例演示了如何创建 TableView 并向其中添加数据。

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 label = new Label("File Data:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //创建表格视图
      TableView<FileData> table = new TableView<FileData>();
      final ObservableList<FileData>数据 = 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")
      );
      //创建列
      TableColumn fileNameCol = new TableColumn("File Name");
      fileNameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
      TableColumn pathCol = new TableColumn("路径");
      pathCol.setCellValueFactory(new PropertyValueFactory("路径"));
      TableColumn sizeCol = new TableColumn("大小");
      sizeCol.setCellValueFactory(new PropertyValueFactory("大小"));
      TableColumn dateCol = new TableColumn("修改日期");
      dateCol.setCellValueFactory(new PropertyValueFactory("修改日期"));
      dateCol.setPrefWidth(100);
      //向表格中添加数据
      ObservableList<String> list = FXCollections.observableArrayList();
      table.setItems(data);
      table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
      table.getColumns().addAll(fileNameCol, pathCol, sizeCol, dateCol);
      //设置表格的大小
      table.setMaxSize(350, 200);
      VBox vbox = new VBox();
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(10, 50, 50, 60));
      vbox.getChildren().addAll(label, table);
      //设置场景
      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 类 −

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

输出


相关文章