You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX中TableView无法显示数据问题求助

Hey there! Let's fix that TableView not showing data issue step by step. I've gone through your code and spotted a few key problems that are causing this:

Key Issues in Your Code

  1. Person class lacks accessible property methods
    The PropertyValueFactory relies on either standard getter methods or JavaFX property accessors to retrieve data from your model class. Your current Person class only defines private properties with no way for the TableView to access them.

  2. Duplicate & incorrect cellValueFactory in FXML
    You've already set up the cellValueFactory in your Controller's initialize method, but the FXML file repeats this configuration with wrong property names (c1/c2 instead of age/name). This creates conflicts and prevents proper data mapping.

  3. Wrong generic type for TableColumn
    Your TableColumns are typed as TableColumn<Person, SimpleStringProperty>, but they should be TableColumn<Person, String> instead. The PropertyValueFactory returns an ObservableValue<String>, not the raw SimpleStringProperty type.


Fixed Code Snippets

1. Updated Person Class

Add getter methods and JavaFX property accessors (the latter is optional but recommended for better JavaFX integration):

package sample;
import javafx.beans.property.SimpleStringProperty;

public class Person {
    private final SimpleStringProperty age;
    private final SimpleStringProperty name;

    public Person(String age, String name) {
        this.age = new SimpleStringProperty(age);
        this.name = new SimpleStringProperty(name);
    }

    // Getters required by PropertyValueFactory
    public String getAge() {
        return age.get();
    }

    public String getName() {
        return name.get();
    }

    // Optional: JavaFX property accessors for more advanced bindings
    public SimpleStringProperty ageProperty() {
        return age;
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }
}

2. Corrected Controller Class

Fix the TableColumn generic types and ensure the cellValueFactory points to the correct Person properties:

package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    ObservableList<Person> people = FXCollections.observableArrayList();

    @FXML
    private TableView<Person> table;
    // Fix generic type to String
    @FXML
    private TableColumn<Person, String> c1;
    @FXML
    private TableColumn<Person, String> c2;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // Match property names to Person's getter methods (getAge() → "age")
        c1.setCellValueFactory(new PropertyValueFactory<>("age"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));

        people.add(new Person("2", "Sam"));
        people.add(new Person("1", "Met"));
        table.setItems(people);
    }
}

3. Cleaned-Up FXML File

Remove the duplicate cellValueFactory entries and fix column text labels for clarity:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <TableView fx:id="table" layoutX="51.0" layoutY="31.0" prefHeight="338.0" prefWidth="498.0">
        <columns>
            <TableColumn fx:id="c1" prefWidth="75.0" text="Age" />
            <TableColumn fx:id="c2" prefWidth="75.0" text="Name" />
        </columns>
    </TableView>
</AnchorPane>

Why This Works

  • The added getter methods let PropertyValueFactory correctly fetch data from your Person objects.
  • Removing duplicate FXML configuration eliminates conflicts and ensures the Controller's setup takes priority.
  • Correcting the TableColumn generic type matches the data type the TableView expects to display, preventing hidden type errors.

内容的提问来源于stack exchange,提问作者WooWings

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:39:55