Phil
Phil

Reputation: 27

JavaFX why TableView don't load data

I just don't get what I'm doing wrong. Everything seems OK, but data just don't wont to load in table. This is controller class of 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'.";


    }
}

This is data class:

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

}

And finally FXML file:

<?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>

I would be very grateful if you can help me and explain from where is mistake.

Upvotes: 0

Views: 790

Answers (1)

James_D
James_D

Reputation: 209684

First, your loadInformationforMeeting() method creates a new TableView, adds data to it, reconfigures the table columns (with the same configuration they already have...) and adds the table columns to the new table.

However, since the new table is not ever placed in the UI, you never see the data. There is no need to create a new table.

Secondly, if you use the existing table (the one defined by the FXML file), it already has the columns attached, and those columns already have cell value factories. So you should not add the columns to the table again, and do not need to repeat the configuration of the columns.

All you need is

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

Upvotes: 1

Related Questions