JavaFX TableView仅显示空行

时间:2017-01-02 17:48:54

标签: oracle javafx tableview

所以我在使用ObViewableList之后遇到了TableView,我得到了空白的行。我认为它与错误的属性绑定有关,但我已经尝试过很多东西但仍然没有运气。 代码有点类似于oracle的例子:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.html

我的代码:

StartingListsTabController.java:

package matchescreation.presentation.startingListsTab;
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.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import matchescreation.model.CurrentTournament;
import matchescreation.model.InvoiceEntry;

public class StartingListsTabController implements Initializable {
@FXML
private TableView<InvoiceEntry> table;
@FXML
private TableColumn<InvoiceEntry, String> colFirstName;
@FXML
private TableColumn<InvoiceEntry, String> colLastName;
@FXML
private ObservableList<InvoiceEntry> dataArray;

@Override
public void initialize(URL location, ResourceBundle resources) {
    table.setColumnResizePolicy(p -> true);
    table.setSortPolicy(e -> false);

    dataArray = FXCollections.observableArrayList();
    table.setItems(dataArray);

    fillInTable();
}

public void fillInTable() {
    InvoiceEntry i = new InvoiceEntry();
    i.setFirstName("aa");
    i.setLastName("bbb");
    dataArray.add(i);

    InvoiceEntry i2 = new InvoiceEntry();
    i2.setFirstName("2aa");
    i2.setLastName("2bbb");
    dataArray.add(i2);
}
}

InvoiceEntry.java:

package matchescreation.model;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class InvoiceEntry {

    private StringProperty firstName;
    private StringProperty lastName;

    public void setFirstName(String value) {
        firstNameProperty().set(value);
    }

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

    public StringProperty firstNameProperty() {
        if (firstName == null) {
            firstName = new SimpleStringProperty(this, "firstName");
        }
        return firstName;
    }

    public void setLastName(String value) {
        lastNameProperty().set(value);
    }

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

    public StringProperty lastNameProperty() {
        if (lastName == null) {
            lastName = new SimpleStringProperty(this, "lastName");
        }
        return lastName;
    }
}

和StartingListsTab.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import java.net.URL?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

<AnchorPane fx:id="rootPane" minHeight="0.0" minWidth="0.0" prefHeight="700.0" prefWidth="1000.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="matchescreation.presentation.startingListsTab.StartingListsTabController">
    <children>
      <VBox prefHeight="514.0" prefWidth="1000.0">
         <children>
            <Label fx:id="titleLabel" text="Tournament title">
               <font>
                  <Font size="36.0" />
               </font>
            </Label>
            <Separator prefWidth="200.0" />
            <Button fx:id="refreshBtn" mnemonicParsing="false" text="refresh" />
            <TableView fx:id="table" prefHeight="200.0" prefWidth="603.0">
              <columns>
                  <TableColumn fx:id="colFirstName" prefWidth="62.0" text="First Name" >
                      <cellValueFactory>
                         <PropertyValueFactory property="firstName" />
                        </cellValueFactory>
                  </TableColumn>
                <TableColumn fx:id="colLastName" prefWidth="616.0" text="Last Name" >
                      <cellValueFactory>
                         <PropertyValueFactory property="lastName" />
                        </cellValueFactory>
                  </TableColumn>

              </columns>
               <VBox.margin>
                  <Insets top="40.0" />
               </VBox.margin>
            </TableView>
         </children>
         <padding>
            <Insets bottom="20.0" left="10.0" right="10.0" top="20.0" />
         </padding>
      </VBox>
    </children>
    <stylesheets>
      <URL value="@style.css" />
   </stylesheets>
</AnchorPane>

0 个答案:

没有答案