我在表格 View 中添加了一个新行,但它没有显示任何值,我不知道为什么?
代码:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
public class TestController implements Initializable {
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private TableView<person> table;
@FXML
void add(ActionEvent event) {
ObservableList<person> data=table .getItems();
data.add(new person("dsdsd", "sdfsdf"));
}
private class person {
private final SimpleStringProperty t1= new SimpleStringProperty("");
private final SimpleStringProperty t2= new SimpleStringProperty("");
public person(String t1,String t2) {
set1Name(t1);
set2Name(t2);
}
public String get1Name() {
return t1.get();
}
public void set1Name(String fName) {
t1.set(fName);
}
public String get2Name() {
return t2.get();
}
public void set2Name(String fName) {
t2.set(fName);
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="schoolmanagement2.TestController">
<children>
<TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="t1" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="C2">
<cellValueFactory>
<PropertyValueFactory property="t2" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
</children>
</AnchorPane>
并且我在输出窗口中也没有收到任何错误 请帮助我如何在表格 View 中添加一行。
谢谢。
最佳答案
出了什么问题
您的问题是您要求反射(reflection)的事情,例如t1
,在遵循 Java 命名约定的 person
类中没有公共(public)访问方法。
因此,在这种情况下,遵循 Java 样式约定不仅可以使您的程序对于习惯阅读 Java 的其他开发人员来说更具可读性,而且还可以使其按照预期的行为工作。
相关问题
这个问题实际上是重复的:Javafx tableview not showing data in all columns ,但是我会在这里添加一个额外的答案,因为它值得一些关于在 FXML 中定义 PropertyValueFactories 的评论。
背景
一个PropertyValueFactory基于使用 Java 反射(reflect)数据类的工作 camel case naming conventions .
请阅读我链接的 PropertyValueFactory javadoc,它解释了它如何与示例配合使用。
虽然数字字符可能允许使用驼峰式大小写,但我并没有真正看到它们被大量使用,而且我不确定它们之间的关系(因为它们既不是大写也不是小写)。因此,我建议替换标识符中的数字字符,在引用的所有数据字段中使用相同的名称,并确保 PropertyValueFactory 反射所需的方法可公开访问。这将使您的示例能够运行。
示例代码
这是您问题中有效的代码版本。
所有代码都应该放在名为formatter
的源码包中。
people.fxml
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="formatter.PeopleTableController">
<children>
<TableView fx:id="table" layoutX="386.0" layoutY="79.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="Given Name">
<cellValueFactory>
<PropertyValueFactory property="firstName" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="Surname">
<cellValueFactory>
<PropertyValueFactory property="surname" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Button layoutX="445.0" layoutY="310.0" mnemonicParsing="false" onAction="#add" text="Button" />
</children>
</AnchorPane>
PeopleApp.java
package formatter;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PeopleApp extends Application {
@Override
public void start(Stage stage) throws Exception{
FXMLLoader loader = new FXMLLoader();
Pane mainPane = loader.load(
getClass().getResourceAsStream(
"people.fxml"
)
);
stage.setScene(
new Scene(
mainPane
)
);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
PeopleTableController.java
package formatter;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
public class PeopleTableController {
@FXML
private TableView<Person> table;
@FXML
void add(ActionEvent event) {
table.getItems().add(new Person("Bill", "Jones"));
}
}
Person.java
package formatter;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
private final StringProperty firstName = new SimpleStringProperty("");
private final StringProperty surname = new SimpleStringProperty("");
public Person(String firstName, String surname) {
setFirstName(firstName);
setSurname(surname);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String name) {
this.firstName.set(name);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getSurname() {
return surname.get();
}
public void setSurname(String name) {
surname.set(name);
}
public StringProperty surnameProperty() {
return surname;
}
}
关于javafx tableview正在添加空行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403279/