迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

JavaFX 中的 setCellValueFactory 方法

作者:迹忆客 最近更新:2023/08/09 浏览次数:

TableView 是任何 UI 中非常重要的一部分; 它有助于为用户可视化数据。 使用表格时,我们使用最常见的方法 setCellValueFactory() 在表格上创建单元格。

在本文中,我们将讨论此方法并查看一个带有解释的示例。


在 JavaFX 中使用 setCellValueFactory 方法

在下面的示例中,我们创建了一个包含一些数据的简单表。 我们示例的代码如下所示。

// Importing all necessary packages

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class FXtable extends Application {
  private TableView table = new TableView(); // Creating a table with a static class "Person"
  private final ObservableList data = FXCollections.observableArrayList(); // Creating an observable list

  private void initData() { // Method that set data to table
    data.setAll(
      // All table datas
      new Person("Alen", "Smith", "alen.smith@example.com"),
      new Person("Stefen", "Johnson", "stefen@example.com"),
      new Person("Uri", "Gagrin", "uri@example.com"),
      new Person("Alex", "Jones", "alex@example.com"),
      new Person("Hexa", "Brown", "hexa@example.com")
    );
  }

   public void start(Stage stage) {
    initData(); // Set initial data to table

    stage.setTitle("JavaFx Table View"); // Set the title of the table
    stage.setWidth(450); // Set the width
    stage.setHeight(500); // Set the height

    Label label = new Label("Simple Address Table"); // Create a label
    label.setFont(new Font("Arial", 20)); // Set the font and font size

    TableColumn FirstNameCol = new TableColumn("First Name");  // Create a column named "First Name"
    FirstNameCol.setMinWidth(100); // Set the minimum column width to 100
    FirstNameCol.setCellValueFactory(new PropertyValueFactory("firstName")); // Populate all the column data for "First Name"

    TableColumn LastNameCol = new TableColumn("Last Name"); // Create a column named "Last Name"
    LastNameCol.setMinWidth(100); // Set the minimum column width to 100
    LastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));  // Populate all the column data for "Last Name"

    TableColumn EmailColl = new TableColumn("Email"); // Create a column named "Email"
    EmailColl.setMinWidth(200); // Set the minimum column width to 200
    EmailColl.setCellValueFactory(new PropertyValueFactory("email")); // Populate all the column data for "Last Name"

    table.setItems(data);
    table.getColumns().addAll(FirstNameCol, LastNameCol, EmailColl); // Add columns to table
    table.setPrefHeight(300); // Set table height


    final VBox vbox = new VBox(10); // Create a VBox
    vbox.setPadding(new Insets(10, 0, 0, 10)); // Add padding
    vbox.getChildren().addAll(label, table); // Organize the VBox with label and table

    stage.setScene(new Scene(new Group(vbox))); // Add the VBox to scene
    stage.show(); // Visualize the scene
  }

  public static class Person {  // Class for creating the person table
    private StringProperty FirstName;
    private StringProperty LastName;
    private StringProperty email;

    private Person ( String FName, String LName, String email )
   {
      this.FirstName = new SimpleStringProperty(FName);
      this.LastName = new SimpleStringProperty(LName);
      this.email = new SimpleStringProperty(email);
    }

    public String getFirstName() {  // Method to get First Name
        return FirstName.get();
    }
    public void setFirstName(String FName) {  // Method to set First Name
        FirstName.set(FName);
    }
    public StringProperty FirstNameProperty() {  // Method to add First Name property
        return FirstName;
    }
    public String getLastName() {  // Method to get Last Name
        return LastName.get();
    }
    public void setLastName(String LName) {  // Method to set Last Name
        LastName.set(LName);
    }
    public StringProperty lastNameProperty() {  // Method to add Last Name property
        return LastName;
    }

    public String getEmail() {  // Method to get Email
        return email.get();
    }
    public void setEmail(String inMail) { // Method to set Email
        email.set(inMail);
    }
    public StringProperty emailProperty() {   // Method to add Email property
        return email;
    }
  }

    public static void main(String[] args) {
        launch(args); } // Launch the application.
}

我们已经在代码中注释了每一行的用途。 现在,我们将在这里讨论 setCellValueFactory() 函数。

通过 FirstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));LastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));EmailColl.setCellValueFactory(new PropertyValueFactory("email")); 行,我们使用了 setCellValueFactory() 方法在我们的表中创建单元格。

在这里,我们创建了名为firstName、lastName 和email 的三列。 此方法指定如何在单个表列中填充表的单元格。

它提供 TableColumn.CellDataFeatures 实例并返回 ObservableValue 实例。 编译上述示例代码并在您的环境中运行后,您将得到以下输出。

输出:

javafx setcellvaluefactory 输出

请记住,如果您的 IDE 不支持自动包含库和包,您可能需要在编译之前手动包含这些必要的库和包。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便