为什么我的 JavaFX TableView 是空的

Joh*_*ang 1 java javafx javafx-2

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

只有一个包调用:controllerJBoxFXTest

第一类MainView.class包controllerJBoxFXTest;

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第2个MainController.java,可以看到我添加了TEST1和TEST2包controllerJBoxFXTest;

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

第三个这是由场景生成器生成的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>
Run Code Online (Sandbox Code Playgroud)

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

Jam*_*s_D 5

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

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

更改单元格值工厂:

iName.setCellValueFactory(new PropertyValueFactory<Table, String>("name"));
Run Code Online (Sandbox Code Playgroud)

或者将类中的方法名称更改TablegetRName()setRName(...)