Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - Why my JavaFX TableView is empty

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 ?

only one package call : 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);
        }
    }
}

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 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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.)

Either change the cell value factory:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...