JavaFX FXML table; why won't my data display

我的未来我决定 提交于 2019-12-24 14:42:49

问题


I'm trying to create a simple table using FXML. The table displays fine, but it's not displaying my data. Here's my main program.

public final class TableTest extends Application {

    @Override
    public void start(final Stage primaryStage) {
       URL fxml = ClassLoader.getSystemClassLoader().getResource("Table.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(fxml);
        TableController tableController = new TableController();
        fxmlLoader.setController(tableController);
        try {
            Pane rootPane = (Pane) fxmlLoader.load();
            Scene scene = new Scene(rootPane);
            primaryStage.setScene(scene);
            primaryStage.show();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    public static void main(final String[] args) {
        launch(args);
    }
}

Here's the FXML.

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <center>
    <TableView fx:id="table">
      <columns>
        <TableColumn text="Column 1" fx:id="col1" minWidth="80.0" />
        <TableColumn text="Column 2" fx:id="col2" minWidth="80.0" />
      </columns>
    </TableView>
  </center>
</BorderPane>

And here's the controller.

public final class TableController implements Initializable {

    @FXML
    private TableView<TableData> table;
    @FXML
    private TableColumn<TableData, String> col1;
    @FXML
    private TableColumn<TableData, String> col2;

    @Override
    public void initialize(final URL url, final ResourceBundle resourceBundle) {

        final ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("C1R1", "C2R1"),
                new TableData("C1R2", "C2R2")
        );
        table.setItems(data);

        col1.setCellValueFactory(
                new PropertyValueFactory<TableData, String>("col1Property"));
        col2.setCellValueFactory(
                new PropertyValueFactory<TableData, String>("col2Property"));
        table.getColumns().setAll(col1, col2);
    }

    private final class TableData {

        private StringProperty col1Property = new SimpleStringProperty();
        private StringProperty col2Property = new SimpleStringProperty();

        public TableData(final String col1, final String col2) {
            col1Property.set(col1);
            col2Property.set(col2);
        }

        public void setCol1(final String col1) {
            col1Property.set(col1);
        }

        public String getCol1() {
            return col1Property.get();
        }

        public StringProperty col1Property() {
            return col1Property;
        }

        public void setCol2(final String col2) {
            col2Property.set(col2);
        }

        public String getCol2() {
            return col2Property.get();
        }

        public StringProperty col2Property() {
            return col2Property;
        }
    }
}

Can anyone tell me what I'm missing?


回答1:


You're not missing anything! In fact you're having too much of something:

new PropertyValueFactory<TableData, String>("col1Property"));

The constructor parameter should be the string value of the variable name of the relevant property without "Property" at the end - i.e., simply "col1" in this example.

See also Can't fill Table with items with javafx as well as the Javadoc for PropertyValueFactory.



来源:https://stackoverflow.com/questions/20074396/javafx-fxml-table-why-wont-my-data-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!