为什么我的JavaFX表视图是空的
我在TableView中添加了默认的TEST1和TEST2作为名称列,但在运行它时没有显示。 这里是我使用的两个类和一个fxml,有人能给我一些关于这个问题的建议吗 只有一个包调用:controllerJBoxFXTest 头等舱 包装控制器JBOxFXTest为什么我的JavaFX表视图是空的,java,javafx,javafx-2,Java,Javafx,Javafx 2,我在TableView中添加了默认的TEST1和TEST2作为名称列,但在运行它时没有显示。 这里是我使用的两个类和一个fxml,有人能给我一些关于这个问题的建议吗 只有一个包调用:controllerJBoxFXTest 头等舱 包装控制器JBOxFXTest import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainView extends Application {
public static void main(String[] args){
Application.launch(MainView.class, (java.lang.String[]) null);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane Page = (AnchorPane) FXMLLoader.load(MainView.class.getResource("MainController.fxml"));
Scene scene = new Scene(Page);
primaryStage.setScene(scene);
primaryStage.setTitle("Test Main");
primaryStage.show();
} catch (Exception e){
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, e);
}
}
}
import java.net.URL;
import java.util.ResourceBundle;
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 javafx.beans.property.SimpleStringProperty;
public class MainController implements Initializable{
@FXML
TableView<Table> tableID;
@FXML
TableColumn<Table, String> iName;
public class Table {
private final SimpleStringProperty rName;
public Table(String sName) {
this.rName = new SimpleStringProperty(sName);
}
public String getName(){
return rName.get();
}
public void setName(String vName) {
this.rName.set(vName);
}
}
final ObservableList<Table> data = FXCollections.observableArrayList(
new Table("TEST1"),
new Table("TEST2")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
iName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
tableID.setItems(data);
}
}
java,您可以看到我添加了TEST1和TEST2
包装控制器JBOxFXTest
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainView extends Application {
public static void main(String[] args){
Application.launch(MainView.class, (java.lang.String[]) null);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane Page = (AnchorPane) FXMLLoader.load(MainView.class.getResource("MainController.fxml"));
Scene scene = new Scene(Page);
primaryStage.setScene(scene);
primaryStage.setTitle("Test Main");
primaryStage.show();
} catch (Exception e){
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, e);
}
}
}
import java.net.URL;
import java.util.ResourceBundle;
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 javafx.beans.property.SimpleStringProperty;
public class MainController implements Initializable{
@FXML
TableView<Table> tableID;
@FXML
TableColumn<Table, String> iName;
public class Table {
private final SimpleStringProperty rName;
public Table(String sName) {
this.rName = new SimpleStringProperty(sName);
}
public String getName(){
return rName.get();
}
public void setName(String vName) {
this.rName.set(vName);
}
}
final ObservableList<Table> data = FXCollections.observableArrayList(
new Table("TEST1"),
new Table("TEST2")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
iName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
tableID.setItems(data);
}
}
import java.net.URL;
导入java.util.ResourceBundle;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.beans.property.SimpleStringProperty;
公共类MainController实现可初始化{
@FXML
TableView-tableID;
@FXML
Tableiname;
公共类表{
私有最终SimpleStringProperty rName;
公共表(字符串sName){
this.rName=新的SimpleStringProperty(sName);
}
公共字符串getName(){
返回rName.get();
}
public void setName(字符串vName){
this.rName.set(vName);
}
}
最终ObservableList数据=FXCollections.observableArrayList(
新表(“TEST1”),
新表(“TEST2”)
);
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
iName.setCellValueFactory(新属性ValueFactory(“rName”);
tableID.setItems(数据);
}
}
第三,这是由场景生成器生成的MainController.fxml
有人能确定为什么我运行MainView.java时TableView是空的吗?
谢谢,您传递给
属性值工厂(rName
)的值与通过get和set方法名称在模型类表中定义的属性不匹配
由于您传递了“rName”,因此PropertyValueFactory
将首先查找名为rNameProperty()
的方法,该方法返回observeValue
。由于不存在,它将查找返回字符串的方法getRName()
。因为它也不存在,所以您的值在列中没有显示。(有关完整说明,请参阅。)
更改单元格值工厂:
iName.setCellValueFactory(new PropertyValueFactory<Table, String>("name"));
iName.setCellValueFactory(新属性值工厂(“名称”);
或者将表中的方法名称更改为getRName()
和setRName(…)
传递给PropertyValueFactory
(rName
)的值与get和set方法名称在模型类表中定义的属性不匹配
由于您传递了“rName”,因此PropertyValueFactory
将首先查找名为rNameProperty()
的方法,该方法返回observeValue
。由于不存在,它将查找返回字符串的方法getRName()
。因为它也不存在,所以您的值在列中没有显示。(有关完整说明,请参阅。)
更改单元格值工厂:
iName.setCellValueFactory(new PropertyValueFactory<Table, String>("name"));
iName.setCellValueFactory(新属性值工厂(“名称”);
或者将表中的方法名称更改为getRName()
和setRName(…)
非常详细的答案。它直接向我指出了这个问题。非常感谢。非常详细的回答。它直接向我指出了这个问题。非常感谢。