JavaFX为什么TableView不加载数据

时间:2017-02-01 10:53:49

标签: javafx load tableview

我只是不明白我做错了什么。一切似乎都没问题,但数据不会在表格中加载。 这是阶段的控制器类:

package application.controllers;

import application.meetings.MeetingData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

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

public class EditMeetingPopUpController {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="dateAndTimeInformation"
    private DatePicker dateAndTimeInformation; // Value injected by FXMLLoader

    @FXML // fx:id="hoursInformation"
    private ComboBox<String> hoursInformation; // Value injected by FXMLLoader

    @FXML // fx:id="minutesInformation"
    private ComboBox<String> minutesInformation; // Value injected by FXMLLoader

    @FXML // fx:id="placeInformation"
    private TextField placeInformation; // Value injected by FXMLLoader

    @FXML // fx:id="categoryInformation"
    private ComboBox<String> categoryInformation; // Value injected by FXMLLoader

    @FXML // fx:id="contactsInformation"
    private ComboBox<String> contactsInformation; // Value injected by FXMLLoader

    @FXML // fx:id="allSelectedContactsTextArea"
    private TextArea allSelectedContactsTextArea; // Value injected by FXMLLoader

    @FXML // fx:id="commentInformation"
    private TextArea commentInformation; // Value injected by FXMLLoader




    // Table variables

    @FXML // fx:id="meetingTable"
    private TableView<MeetingData> meetingTable; // Value injected by FXMLLoader

    @FXML // fx:id="dateAntTimeColumn"
    private TableColumn<MeetingData, String> dateAntTimeColumn; // Value injected by FXMLLoader

    @FXML // fx:id="placeColumn"
    private TableColumn<MeetingData, String> placeColumn; // Value injected by FXMLLoader

    @FXML // fx:id="categoryColumn"
    private TableColumn<MeetingData, String> categoryColumn; // Value injected by FXMLLoader

    @FXML // fx:id="contactsColumn"
    private TableColumn<MeetingData, String> contactsColumn; // Value injected by FXMLLoader

    @FXML // fx:id="commentsColumn"
    private TableColumn<MeetingData, String> commentsColumn; // Value injected by FXMLLoader

    @FXML // Add more contactsColumn to contactsColumn list
    void addContactToAllContactsTextArea(ActionEvent event) {

    }

    @FXML
    void editMeeting(ActionEvent event) {

    }

    @FXML
    void loadInformationForMeeting(ActionEvent event) {
        ObservableList<MeetingData> data = getMeetingData();

        this.meetingTable = new TableView<>();
        this.meetingTable.setItems(data);

        this.placeColumn.setCellValueFactory(new PropertyValueFactory<MeetingData, String>("date"));
        this.placeColumn.setCellValueFactory(new PropertyValueFactory<MeetingData, String>("place"));
        this.categoryColumn.setCellValueFactory(new PropertyValueFactory<MeetingData, String>("category"));
        this.contactsColumn.setCellValueFactory(new PropertyValueFactory<MeetingData, String>("contacts"));
        this.commentsColumn.setCellValueFactory(new PropertyValueFactory<MeetingData, String>("comments"));

        this.meetingTable.getColumns().add(this.dateAntTimeColumn);
        this.meetingTable.getColumns().add(this.placeColumn);
        this.meetingTable.getColumns().add(this.categoryColumn);
        this.meetingTable.getColumns().add(this.contactsColumn);
        this.meetingTable.getColumns().add(this.commentsColumn);


//        // TODO: 01-Feb-17 delete this after finish
//        for (MeetingData meetingData : data) {
//            System.out.println(meetingData.getDate());
//            System.out.println(meetingData.getPlace());
//            System.out.println(meetingData.getCategory());
//            System.out.println(meetingData.getContacts());
//            System.out.println(meetingData.getComments());
//            System.out.println("----- END -----");
//            System.out.println();
//        }
    }

    // get all meetings
    public ObservableList<MeetingData> getMeetingData() {
        ObservableList<MeetingData> data = FXCollections.observableArrayList();
        data.add(new MeetingData("sdzgzrdbf", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));
        data.add(new MeetingData("sEDGdb", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));
        data.add(new MeetingData("sdzgsbfgzrdbf", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));
        data.add(new MeetingData("zsrbd", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));
        data.add(new MeetingData("rbdbdb", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));
        data.add(new MeetingData("rsbdbd", "sgzsfbs", "EDbfb", "sbdbf", "zbdd"));

        return data;
    }

    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        assert dateAndTimeInformation != null : "fx:id=\"dateAndTimeInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert hoursInformation != null : "fx:id=\"hoursInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert minutesInformation != null : "fx:id=\"minutesInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert placeInformation != null : "fx:id=\"placeInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert categoryInformation != null : "fx:id=\"categoryInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert contactsInformation != null : "fx:id=\"contactsInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert allSelectedContactsTextArea != null : "fx:id=\"allSelectedContactsTextArea\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert commentInformation != null : "fx:id=\"commentInformation\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert meetingTable != null : "fx:id=\"meetingTable\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert dateAntTimeColumn != null : "fx:id=\"dateAntTimeColumn\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert placeColumn != null : "fx:id=\"placeColumn\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert categoryColumn != null : "fx:id=\"categoryColumn\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert contactsColumn != null : "fx:id=\"contactsColumn\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";
        assert commentsColumn != null : "fx:id=\"commentsColumn\" was not injected: check your FXML file 'EditMeetingPopUp.fxml'.";


    }
}

这是数据类:

package application.meetings;

public class MeetingData {

    // Fields
    private String date;
    private String place;
    private String category;
    private String contacts;
    private String comments;

    // Constructors
    public MeetingData(String date, String place, String category, String contacts, String comments) {
        this.date = date;
        this.place = place;
        this.category = category;
        this.contacts = contacts;
        this.comments = comments;
    }

    // Setters
    public void setDate(String date) {
        this.date = date;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setContacts(String contacts) {
        this.contacts = contacts;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    // Getters
    public String getDate() {
        return date;
    }

    public String getPlace() {
        return place;
    }

    public String getCategory() {
        return category;
    }

    public String getContacts() {
        return contacts;
    }

    public String getComments() {
        return comments;
    }

}

最后是FXML文件:

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controllers.EditMeetingPopUpController">
   <bottom>
      <HBox alignment="CENTER_RIGHT" BorderPane.alignment="CENTER">
         <children>
            <Button mnemonicParsing="false" onAction="#loadInformationForMeeting" text="Зареди информация">
               <HBox.margin>
                  <Insets right="10.0" />
               </HBox.margin>
            </Button>
            <Button defaultButton="true" mnemonicParsing="false" onAction="#editMeeting" prefWidth="80.0" text="Редактирай">
               <HBox.margin>
                  <Insets />
               </HBox.margin></Button>
         </children>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </HBox>
   </bottom>
   <center>
      <VBox alignment="CENTER">
         <children>
            <HBox alignment="CENTER_LEFT">
               <children>
                  <Label prefWidth="70.0" text="Дата" />
                  <DatePicker fx:id="dateAndTimeInformation" prefWidth="220.0" promptText="Дата" showWeekNumbers="true">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                     <tooltip>
                        <Tooltip text="Полето е задължително" />
                     </tooltip>
                  </DatePicker>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox alignment="CENTER_LEFT">
               <children>
                  <Label prefWidth="70.0" text="Час" />
                  <ComboBox fx:id="hoursInformation" prefWidth="70.0">
                     <HBox.margin>
                        <Insets left="20.0" right="5.0" />
                     </HBox.margin>
                     <tooltip>
                        <Tooltip text="Полето е задължително" />
                     </tooltip>
                  </ComboBox>
                  <Label text="часa">
                     <HBox.margin>
                        <Insets right="20.0" />
                     </HBox.margin>
                  </Label>
                  <ComboBox fx:id="minutesInformation" prefWidth="70.0">
                     <HBox.margin>
                        <Insets right="5.0" />
                     </HBox.margin>
                     <tooltip>
                        <Tooltip text="Полето е задължително" />
                     </tooltip>
                  </ComboBox>
                  <Label text="мин.">
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                  </Label>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="218.0">
               <children>
                  <Label prefWidth="70.0" text="Място" />
                  <TextField fx:id="placeInformation" prefWidth="220.0" promptText="Място на срещата">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                  </TextField>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="253.0">
               <children>
                  <Label prefWidth="70.0" text="Категория" />
                  <ComboBox fx:id="categoryInformation" prefWidth="220.0" promptText="Изберете категория">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                  </ComboBox>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="240.0">
               <children>
                  <Label prefWidth="70.0" text="Участници" />
                  <ComboBox fx:id="contactsInformation" onAction="#addContactToAllContactsTextArea" prefWidth="220.0" promptText="Изберете участници">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                     <tooltip>
                        <Tooltip text="Полето е задължително" />
                     </tooltip>
                  </ComboBox>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox>
               <children>
                  <Pane prefHeight="100.0" prefWidth="70.0" />
                  <TextArea fx:id="allSelectedContactsTextArea" editable="false" prefHeight="100.0" prefWidth="220.0" promptText="Участници в срещата">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                  </TextArea>
               </children>
               <VBox.margin>
                  <Insets bottom="10.0" />
               </VBox.margin>
            </HBox>
            <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="293.0">
               <children>
                  <Label alignment="TOP_LEFT" prefHeight="100.0" prefWidth="70.0" text="Коментар" />
                  <TextArea fx:id="commentInformation" prefHeight="100.0" prefWidth="220.0" promptText="Въведете коментар">
                     <HBox.margin>
                        <Insets bottom="10.0" left="20.0" />
                     </HBox.margin>
                     <tooltip>
                        <Tooltip text="Полето е задължително" />
                     </tooltip>
                  </TextArea>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </HBox>
         </children>
      </VBox>
   </center>
   <left>
      <VBox BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </VBox>
   </left>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
   <top>
      <Label alignment="CENTER" prefWidth="600.0" text="Моля изберете среща за редактиране">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
         <BorderPane.margin>
            <Insets bottom="10.0" left="360.0" />
         </BorderPane.margin>
      </Label>
   </top>
   <right>
      <TableView fx:id="meetingTable" editable="true" prefWidth="600.0" BorderPane.alignment="CENTER">
        <columns>
            <TableColumn fx:id="dateAntTimeColumn" prefWidth="75.0" text="Дата и час" >
                <cellValueFactory>
                   <PropertyValueFactory property="date" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="placeColumn" prefWidth="75.0" text="Място" >
               <cellValueFactory>
                  <PropertyValueFactory property="place" />
               </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="categoryColumn" prefWidth="75.0" text="Категория" >
               <cellValueFactory>
                  <PropertyValueFactory property="category" />
               </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="contactsColumn" prefWidth="75.0" text="Участници" >
               <cellValueFactory>
                  <PropertyValueFactory property="contacts" />
               </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="commentsColumn" prefWidth="114.0" text="Коментар" >
               <cellValueFactory>
                  <PropertyValueFactory property="comments" />
               </cellValueFactory>
            </TableColumn>
        </columns>
         <BorderPane.margin>
            <Insets bottom="10.0" />
         </BorderPane.margin>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </right>
</BorderPane>

如果你能帮助我并解释错误在哪里,我将非常感激。

1 个答案:

答案 0 :(得分:1)

首先,您的loadInformationforMeeting()方法会创建 TableView,向其添加数据,重新配置表列(使用他们已有的相同配置...)和将表列添加到新表中。

但是,由于新表未放置在UI中,因此您永远不会看到数据。无需创建新表。

其次,如果您使用现有表(由FXML文件定义的表),它已经附加了列,并且这些列已经具有单元值工厂。因此,您不应该再次将列添加到表中,也不需要重复列的配置。

你需要的只是

@FXML
void loadInformationForMeeting(ActionEvent event) {
    ObservableList<MeetingData> data = getMeetingData();
    this.meetingTable.setItems(data);
}
相关问题