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
Person class lacks accessible property methods
ThePropertyValueFactoryrelies on either standard getter methods or JavaFX property accessors to retrieve data from your model class. Your currentPersonclass only defines private properties with no way for the TableView to access them.Duplicate & incorrect
cellValueFactoryin FXML
You've already set up thecellValueFactoryin your Controller'sinitializemethod, but the FXML file repeats this configuration with wrong property names (c1/c2instead ofage/name). This creates conflicts and prevents proper data mapping.Wrong generic type for TableColumn
Your TableColumns are typed asTableColumn<Person, SimpleStringProperty>, but they should beTableColumn<Person, String>instead. ThePropertyValueFactoryreturns anObservableValue<String>, not the rawSimpleStringPropertytype.
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
PropertyValueFactorycorrectly fetch data from yourPersonobjects. - 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

