【问题标题】:Why my JavaFX TableView is empty为什么我的 JavaFX TableView 是空的
【发布时间】:2014-10-19 07:08:50
【问题描述】:

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

只有一个包调用:controllerJBoxFXTest

第一类 MainView.class 包控制器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 包控制器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 是空的? 谢谢,

【问题讨论】:

    标签: java javafx javafx-2


    【解决方案1】:

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

    由于您传递了“rName”,PropertyValueFactory 将首先查找名为rNameProperty() 的方法,返回一个ObservableValue&lt;String&gt;。由于不存在,它会寻找一个方法getRName(),返回一个String。由于这也不存在,因此您在列中没有任何价值。 (有关完整说明,请参阅Javadocs。)

    要么更改单元格值工厂:

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

    或者将Table类中的方法名改为getRName()setRName(...)

    【讨论】:

    • 非常详细的答案。它直接指出了我的问题。非常感谢。
    猜你喜欢
    • 2017-09-28
    • 2013-03-13
    • 2018-09-25
    • 2020-06-16
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多