As shown in the following code I tried to add some data I to the table. But when I run the application it only just show the empty table. What is the possible reason to that problem ?

package com.fg.transbridge.tool.ui;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.property.SimpleStringProperty;
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;

public class TableViewController implements Initializable {

    @FXML
    private TableColumn<Person, String> colX;
    @FXML
    private TableColumn<Person, String> colY;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

        final TableView<Person> table = new TableView<Person>();
        final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob", "Smith"), new Person("Isabella", "Johnson"), new Person("Ethan", "Williams"),
                new Person("Emma", "Jones"), new Person("Michael", "Brown"));

        colX.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));

        colY.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

        table.setItems(data);
        table.getColumns().addAll(colX, colY);

    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;

        private Person(String fName, String lName) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);

        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

    }

}

FXML file has the following code and some more :

TableView.fxml

<TableView layoutX="172.0" layoutY="106.0" prefHeight="200.0" prefWidth="200.0">
      <columns>
        <TableColumn prefWidth="75.0" text="Column X" fx:id="colX" />
        <TableColumn prefWidth="75.0" text="Column Y" fx:id="colY"/>
      </columns>
    </TableView>
有帮助吗?

解决方案

You are creating and publishing data to a table that never been referenced in your FXML file and never been visible in the node graph.

You have to add a fx:id Attribute to the TableView element:

<TableView fx:id= "table "layoutX="172.0" layoutY="106.0" prefHeight="200.0" prefWidth="200.0">

Reference the table in your controller

@FXML    
TableView<Person> table

Remove the final TableView<Person> table = new TableView<Person>(); the FXMLLoader will initialize all the components for you.

See this example of using TableView and FXML.

其他提示

your problem is simple. you create a new TableView but the colum are already on the tableview in your fxml. Just delete :

final TableView<Person> table = new TableView<Person>();

and add

@FXML
private TableView table;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top