java - 为什么我的 JavaFX TableView 是空的

标签 java javafx javafx-2

我在 TableView 中添加了默认的 TEST1 和 TEST2 作为 Name Col,但是当我运行它时没有显示。 这是我使用的两个类和一个 fxml,任何人都可以给我一些关于这个问题的建议吗?

只有一个包调用:controllerJBoxFXTest

第一类 MainView.class 包 Controller 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);
        }
    }
}

第二个MainController.java,你可以看到我添加了TEST1和TEST2 包 Controller JBoxFXTest;

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);
    }
}

3rd 这是场景生成器生成的MainController.fxml

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllerJBoxFXTest.MainController">
   <children>
      <SplitPane dividerPositions="0.8729096989966555" layoutX="197.0" layoutY="61.0" orientation="VERTICAL" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <TableView fx:id="tableID" layoutX="224.0" layoutY="107.0" prefHeight="519.0" prefWidth="798.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn fx:id="iName" prefWidth="769.0" text="Name" />
                    </columns>
                  </TableView>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
        </items>
      </SplitPane>
   </children>
</AnchorPane>

有人能确定为什么我运行 MainView.java 时我的 TableView 是空的吗? 谢谢,

最佳答案

您传递给 PropertyValueFactory 的值( rName ) 与模型类 Table 中定义的属性 不匹配通过 get 和 set 方法名称。

由于您传递了“rName”,PropertyValueFactory将首先寻找一个名为 rNameProperty() 的方法返回 ObservableValue<String> .由于不存在,它将寻找一个方法 getRName()返回 String .由于它也不存在,因此您没有任何值(value)可以显示在该列中。 (有关完整说明,请参阅 Javadocs。)

要么更改单元格值工厂:

iName.setCellValueFactory(new PropertyValueFactory<Table, String>("name"));

或更改 Table 中的方法名称类别为 getRName()setRName(...) .

关于java - 为什么我的 JavaFX TableView 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25497671/

相关文章:

java - org.springframework.remoting.RemoteAccessException : Could not access HTTP invoker remote service occurs when starting tasks from anbother thread

combobox - javafx-2,仅在回车键上设置可编辑的组合框值

java - Android Studio SDK 路径包含重音符号

java - 如何在 Spring MVC 中映射 JSON 对象?

java - 如何使用 Java 代码获取处理器每个核心的当前利用率百分比

java - 使用 HTTP Commons Client 的基本身份验证

javafx - 如何在 GSON 中反序列化 Kotlin 代表

JavaFX ListView 为文本着色

java - axis.getCategories().remove(index number) 在 JavaFX 图表中不起作用

java - 将更改监听器注册到 javafx 中的节点组