为什么我的JavaFX TableView是空的 [英] Why my JavaFX TableView is empty

查看:188
本文介绍了为什么我的JavaFX TableView是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I added default TEST1 and TEST2 as Name Col in TableView but didn't display when I run it. Here are two classes and one fxml I used for, can anyone give me some advices regarding this issue ?

只有一个包调用:controllerJBoxFXTest

only one package call : controllerJBoxFXTest

第一类MainView.class
包controllerJBoxFXTest;

1st Class MainView.class package 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;

2nd MainController.java, you can see I added TEST1 and TEST2 package 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生成器

3rd This is MainController.fxml which was generated by scene generator

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

Can anyone sure why my TableView is empty when I run the MainView.java ? Thanks,

推荐答案

您传递给 PropertyValueFactory的价值 rName )与模型类中定义的属性不匹配获取并设置方法名称。

The value you passed to the PropertyValueFactory (rName) does not match the property defined in your model class Table by the get and set method names.

由于您传递了rName, PropertyValueFactory 将首先查找名为的方法rNameProperty()返回 ObservableValue< String> 。由于不存在,它将查找方法 getRName()返回 String 。由于它既不存在,也没有值显示在列中。 (请参阅 Javadocs 一个完整的描述。)

Since you passed "rName", the PropertyValueFactory will first look for a method called rNameProperty() returning an ObservableValue<String>. Since none exists, it will look for a method getRName() returning a String. Since that doesn't exist either, your have no value to display in the column. (See the Javadocs for a full description.)

更改单元格值工厂:

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

或更改表中的方法名称 class to getRName() setRName(...)

or change the method names in the Table class to getRName() and setRName(...).

这篇关于为什么我的JavaFX TableView是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆