无法在javafx中将数据填充到TableView

时间:2016-07-17 21:21:25

标签: javafx-2 tableview

我是javafx的新手,我正在为图书馆开发一个小应用程序,应该在表格中显示一些数据,但无论如何数据都没有显示在表格中!我已经搜索并完全像它应该的那样,仍然没有填充。

注意:DBConnection是一个连接数据库的类,我可以毫无问题地从中读取数据。

主要课程:

package app;

import Connector.DBConnection;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.sql.Connection;

public class Main extends Application {
    private Connection connection;
    private Parent root;

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.getIcons().add(new Image(String.valueOf(this.getClass().getResource("icon.png"))));
      /*  if(!logIn())
            return;*/
        connection = DBConnection.getActiveConnection();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("main_screen.fxml"));
    //    loader.setLocation(getClass().getResource("main_screen.fxml"));
        Parent root = (VBox) loader.load();


        MainMenuController menuController = loader.getController();

        menuController.setConnection(connection);
        menuController.init();
        menuController.fillTable();

     //   Parent root = FXMLLoader.load(getClass().getResource("main_screen.fxml"));

        primaryStage.setTitle("Hello World");

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

控制器类:

package app;


import com.sun.org.omg.CORBA.Initializer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import models.Book;
import models.Borrower;
import models.History;

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

public class MainMenuController {
    private Connection connection;
    private final int BOOK_VEIW_COLUMN_COUNT = 9;
    private ObservableList<Book> bookData;

    @FXML
    private TableView<Book> bookView;
    @FXML
    private TableView<History> historyView;
    @FXML
    private TableView<Borrower> borrowerView;
    @FXML
    private TableColumn<Book, Integer> first;
    @FXML
    private TableColumn<Book, String> second;
    @FXML
    private TableColumn<Book, String> third;
    @FXML
    private TableColumn<Book, String> forth;
    @FXML
    private TableColumn<Book, String> fifth;
    @FXML
    private TableColumn<Book, String> sixth;
    @FXML
    private TableColumn<Book, Integer> seventh;
    @FXML
    private TableColumn<Book, Integer> eighth;
    @FXML
    private TableColumn<Book, Integer> ninth;

    public void init(){
        bookData = FXCollections.observableArrayList();
        bookView = new TableView<>();
        first.setCellValueFactory(cellData -> cellData.getValue().numberProperty().asObject());
        second.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        third.setCellValueFactory(cellData -> cellData.getValue().authorProperty());
        forth.setCellValueFactory(cellData -> cellData.getValue().mainTopicProperty());
        fifth.setCellValueFactory(cellData -> cellData.getValue().secondaryTopicProperty());
        sixth.setCellValueFactory(cellData -> cellData.getValue().divisionProperty());
        seventh.setCellValueFactory(cellData -> cellData.getValue().codeNumberProperty().asObject());
        eighth.setCellValueFactory(cellData -> cellData.getValue().copiesProperty().asObject());
        ninth.setCellValueFactory(cellData -> cellData.getValue().availCopiesProperty().asObject());
    }

    public void setConnection(Connection connection){
        this.connection = connection;
    }

    public void fillTable(){
        try {
            String sql = "select * from books";
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet set = statement.executeQuery();
            int currNumber = 1;
            while(set.next()){
                Book book = new Book.BookBuilder()
                        .setAuthor(set.getString("author"))
                        .setAvailCopies(set.getInt("available copies"))
                        .setDivision(set.getString("division"))
                        // .setHistory(set.getString(""))
                        .setSecondaryTopic(set.getString("secondary topic"))
                        .setCopies(set.getInt("copies"))
                        .setID(set.getInt("id"))
                        .setNumber(currNumber++)
                        .setName(set.getString("name"))
                        .setMainTopic(set.getString("main topic"))
                        .setCodeNumber(set.getInt("code number"))
                        .build();
                bookData.add(book);
            }
      //      bookView.getColumns().addAll(first, second, third, forth, fifth, sixth, seventh, eighth, ninth);
            bookView.setItems(bookData);

            //       bookView.getColumns().addAll(first, second, third, forth, fifth, sixth, seventh, eighth, ninth);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

fxml文件:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.cell.PropertyValueFactory?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="619.0" prefWidth="1019.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.MainMenuController">
   <children>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="150.0" prefWidth="600.0">
                       <children>
                          <TableView fx:id="bookView" editable="true" layoutX="200.0" layoutY="-40.0" nodeOrientation="RIGHT_TO_LEFT" prefHeight="302.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                            <columns>
                                <TableColumn fx:id="first" prefWidth="65.0" text="عدد" />
                              <TableColumn fx:id="second" prefWidth="175.0" text="الاسم" />
                              <TableColumn fx:id="third" prefWidth="61.0" text="المؤلف" />
                                <TableColumn fx:id="forth" prefWidth="75.0" text="الموضوع الرئيسى" />
                                <TableColumn fx:id="fifth" prefWidth="75.0" text="الموضوع الفرعى" />
                                <TableColumn fx:id="sixth" prefWidth="75.0" text="رقم التقسيم" />
                                <TableColumn fx:id="seventh" prefWidth="75.0" text="الرقم الكودى" />
                                <TableColumn fx:id="eighth" prefWidth="75.0" text="عدد النسخ" />
                                <TableColumn fx:id="ninth" prefWidth="339.0" text="النسخ المتوفره" />
                            </columns>
                             <columnResizePolicy>
                                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                             </columnResizePolicy>
                          </TableView>
                       </children>
                    </AnchorPane>
   </children>
</VBox>

图书课程:&#34;使用构建器模式&#34;

package models;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * Created by Andy on 6/21/2016.
 */
public class Book {
    private IntegerProperty number;
    private StringProperty name;
    private StringProperty author;
    private StringProperty mainTopic;
    private StringProperty secondaryTopic;
    private StringProperty division;
    private IntegerProperty codeNumber;
    private IntegerProperty ID;
    private IntegerProperty copies;
    private IntegerProperty availCopies;
    private ObservableList<History> history = FXCollections.observableArrayList();

    public Book(IntegerProperty number, StringProperty name, StringProperty author, StringProperty mainTopic, StringProperty secondaryTopic, StringProperty division, IntegerProperty codeNumber, IntegerProperty ID, IntegerProperty copies, IntegerProperty availCopies, ObservableList<History> history) {
        this.number = number;
        this.name = name;
        this.author = author;
        this.mainTopic = mainTopic;
        this.secondaryTopic = secondaryTopic;
        this.division = division;
        this.codeNumber = codeNumber;
        this.ID = ID;
        this.copies = copies;
        this.availCopies = availCopies;
        this.history = history;
    }

    public int getNumber() {
        return number.get();
    }

    public IntegerProperty numberProperty() {
        return number;
    }

    public void setNumber(int number) {
        this.number.set(number);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getAuthor() {
        return author.get();
    }

    public StringProperty authorProperty() {
        return author;
    }

    public void setAuthor(String author) {
        this.author.set(author);
    }

    public String getMainTopic() {
        return mainTopic.get();
    }

    public StringProperty mainTopicProperty() {
        return mainTopic;
    }

    public void setMainTopic(String mainTopic) {
        this.mainTopic.set(mainTopic);
    }

    public String getSecondaryTopic() {
        return secondaryTopic.get();
    }

    public StringProperty secondaryTopicProperty() {
        return secondaryTopic;
    }

    public void setSecondaryTopic(String secondaryTopic) {
        this.secondaryTopic.set(secondaryTopic);
    }

    public String getDivision() {
        return division.get();
    }

    public StringProperty divisionProperty() {
        return division;
    }

    public void setDivision(String division) {
        this.division.set(division);
    }

    public int getCodeNumber() {
        return codeNumber.get();
    }

    public IntegerProperty codeNumberProperty() {
        return codeNumber;
    }

    public void setCodeNumber(int codeNumber) {
        this.codeNumber.set(codeNumber);
    }

    public int getID() {
        return ID.get();
    }

    public IntegerProperty IDProperty() {
        return ID;
    }

    public void setID(int ID) {
        this.ID.set(ID);
    }

    public int getCopies() {
        return copies.get();
    }

    public IntegerProperty copiesProperty() {
        return copies;
    }

    public void setCopies(int copies) {
        this.copies.set(copies);
    }

    public int getAvailCopies() {
        return availCopies.get();
    }

    public IntegerProperty availCopiesProperty() {
        return availCopies;
    }

    public void setAvailCopies(int availCopies) {
        this.availCopies.set(availCopies);
    }

    public ObservableList<History> getHistory() {
        return history;
    }

    public void setHistory(ObservableList<History> history) {
        this.history = history;
    }

    public static class BookBuilder{
        private IntegerProperty number;
        private StringProperty name;
        private StringProperty author;
        private StringProperty mainTopic;
        private StringProperty secondaryTopic;
        private StringProperty division;
        private IntegerProperty codeNumber;
        private IntegerProperty ID;
        private IntegerProperty copies;
        private IntegerProperty availCopies;
        private ObservableList<History> history = FXCollections.observableArrayList();
        private final Integer DEFAULT_COPIES_NUMBER = 1;

        public BookBuilder(){
            name = new SimpleStringProperty("");
            author = new SimpleStringProperty("");
            mainTopic = new SimpleStringProperty("");
            secondaryTopic = new SimpleStringProperty("");
            division = new SimpleStringProperty();
            codeNumber = new SimpleIntegerProperty();
            ID = new SimpleIntegerProperty();
            copies = new SimpleIntegerProperty(DEFAULT_COPIES_NUMBER);
            availCopies = new SimpleIntegerProperty(DEFAULT_COPIES_NUMBER);
            number = new SimpleIntegerProperty(DEFAULT_COPIES_NUMBER);
        }


        public BookBuilder setNumber(int number) {
            this.number.set(number);
            return this;
        }

        public BookBuilder setName(String name) {
            this.name.set(name);
            return this;
        }

        public BookBuilder setAuthor(String author) {
            this.author.set(author);
            return this;
        }

        public BookBuilder setMainTopic(String mainTopic) {
            this.mainTopic.set(mainTopic);
            return this;
        }

        public BookBuilder setSecondaryTopic(String secondaryTopic) {
            this.secondaryTopic.set(secondaryTopic);
            return this;
        }

        public BookBuilder setDivision(String division) {
            this.division.set(division);
            return this;
        }

        public BookBuilder setCodeNumber(int codeNumber) {
            this.codeNumber.set(codeNumber);
            return this;
        }

        public BookBuilder setID(int ID) {
            this.ID.set(ID);
            return this;
        }

        public BookBuilder setCopies(int copies) {
            this.copies.set(copies);
            return this;
        }

        public BookBuilder setAvailCopies(int availCopies) {
            this.availCopies.set(availCopies);
            return this;
        }

        public BookBuilder setHistory(ObservableList<History> history) {
            this.history = history;
            return this;
        }

        public Book build(){
            return new Book(number, name, author, mainTopic, secondaryTopic, division, codeNumber, ID, copies, availCopies, history);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在控制器的init方法中

bookView = new TableView<>();

但是你已经有了FXML加载器创建的TableView!从现在开始,您在bookView上执行的所有操作都是在此新实例上进行的,而不是在场景中显示的操作。这意味着您永远不会将项目添加到您看到的TableView,而是添加到不同的表格视图中,从未显示过。

删除此行,您的问题应该解决。