JavaFX为什么TableView不';t加载数据

JavaFX为什么TableView不';t加载数据,javafx,load,tableview,Javafx,Load,Tableview,我只是不明白我做错了什么。看起来一切正常,但数据不习惯加载到表中。 这是stage的控制器类: package application.controllers; import application.meetings.MeetingData; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import java

我只是不明白我做错了什么。看起来一切正常,但数据不习惯加载到表中。 这是stage的控制器类:

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'.";


    }
}
最后是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>


如果您能帮助我并解释错误的位置,我将不胜感激。

首先,您的
loadInformationforMeeting()
方法创建一个新的
,向其添加数据,重新配置表列(与它们已有的配置相同…)并将表列添加到新表中

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

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

你所需要的只是

@FXML
void loadInformationForMeeting(ActionEvent event) {
    ObservableList<MeetingData> data = getMeetingData();
    this.meetingTable.setItems(data);
}
@FXML
无效加载信息会议(ActionEvent事件){
ObservableList data=getMeetingData();
此.meetingTable.setItems(数据);
}

如何使用它?如果我试图在控制台上打印Observable中的内容,请列出它的工作情况,但无法加载到表中。我用SimpleStringProperty更改了MeetingData类字符串,创建了defaul构造函数,更改了getter和setter,而且什么都没有发生。表中正在写“表中没有内容”。我尝试了,再次没有更改
this.meetingTable=new TableView()?????参见,例如。
@FXML
void loadInformationForMeeting(ActionEvent event) {
    ObservableList<MeetingData> data = getMeetingData();
    this.meetingTable.setItems(data);
}