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


第二个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);
    }
}


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为空?
谢谢,

最佳答案

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

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

更改单元格值工厂:

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


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

09-16 06:01