无法填充在SceneBuilder 2中创建的JavaFX TableView

时间:2015-01-06 20:08:15

标签: javafx javafx-2 scenebuilder

我正在尝试使用场景构建器2重新创建table view sample,但无法填充我的TableView。

我在JavaFx SceneBuilder中定义了我的表,如下所示:

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

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

<AnchorPane id="AnchorPane" prefHeight="380.0" prefWidth="462.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="tableviewsample.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="325.0" layoutY="324.0" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
        <TableView fx:id="table" layoutX="26.0" layoutY="29.0" prefHeight="221.0" prefWidth="411.0">
            <columns>
                <TableColumn fx:id="firstNameCol" prefWidth="75.0" text="First Name" />
                <TableColumn fx:id="lastNameCol" prefWidth="75.0" text="Last Name" />
                <TableColumn fx:id="emailCol" prefWidth="75.0" text="Email" />
            </columns>
        </TableView>
        <TextField fx:id="addFirstName" layoutX="26.0" layoutY="284.0" prefHeight="25.0" prefWidth="81.0" promptText="First Name" />
        <TextField fx:id="addLastName" layoutX="121.0" layoutY="284.0" prefHeight="25.0" prefWidth="89.0" promptText="Last Name" />
        <TextField fx:id="addEmail" layoutX="222.0" layoutY="284.0" promptText="Email" />
    </children>
</AnchorPane>

我的控制器代码:

package tableviewsample;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private TableView<Person> table;// = new TableView<Person>();

    @FXML private TableColumn firstNameCol ;
    @FXML private TableColumn lastNameCol ;
    @FXML private TableColumn emailCol ;

    @FXML TextField addFirstName;
    @FXML TextField addLastName;
    @FXML TextField addEmail;

    private final ObservableList<Person> data
            = FXCollections.observableArrayList(
                    new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
            );

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        data.add(new Person(
                addFirstName.getText(),
                addLastName.getText(),
                addEmail.getText()));
        addFirstName.clear();
        addLastName.clear();
        addEmail.clear();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        firstNameCol.setMinWidth(100);
        lastNameCol.setMinWidth(100);
        emailCol.setMinWidth(200);
        table.getItems().setAll(this.data);
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

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

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

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
}

主要代码:

package tableviewsample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TableViewSample extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

截图:

screenshot

当我运行它时,表中有空行。如果我在3个TextFields中键入一些值并单击按钮,则表中仍未插入值。 你能找到代码有什么问题吗?另外,在不相关的说明中,如何使表格可滚动?

3 个答案:

答案 0 :(得分:3)

您尚未在cellValueFactory上设置任何TableColumntutorial you linked中的示例12-5)。

您可以使用

在FXML中执行此操作
<TableColumn fx:id="firstNameCol" prefWidth="75.0" text="First Name" >
    <cellValueFactory><PropertyValueFactory property="firstName"/></cellValueFactory>
</TableColumn>

或带有

的控制器
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));

(和其他列类似)

答案 1 :(得分:3)

你实际上犯了两个错误。

首先,您尝试使用getItems().setAll()将数据模型分配给TableView。看看the documentation of setAll()。它将给定列表的所有值复制到ObservableList中。这不是您想要的,您希望TableView开始观察data并响应更改。这样做的方法是直接通过setItems(data)分配引用。

查看TableView文档中的代码示例。您忘记将Person中的各种StringProperties连接到列。这就是他们这样做的方式:

firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

最后,我有一点注意事项。请注意,这不是您特定问题的原因。

我看到您正在使用raw types。这通常被认为是一件坏事,你可能会从编译器那里得到一些关于它的警告。您可以通过声明对象使用的泛型类型来解决问题。请注意,在上面的代码示例中,我为PropertyValueFactories声明TableView.items中包含的类的类型为Person,并且行中显示的对象类型为String

对于你也要求的滚动,一旦TableView中有足够的行,它就会自动执行。

答案 2 :(得分:0)

在IN鼠标按钮上您可以编写: / setItems(); 遵循上面的绿色勾号