0

My TableView doesn't show any String, but it has Clickable (Exist, but shows no words).

Database used: MySQL.

Full Controller:

package application.BookList;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
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.cell.PropertyValueFactory;

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

public class Controller implements Initializable{
    String theConnectionDriver = "com.mysql.jdbc.Driver";
    private String theConnectionString = "jdbc:mysql://localhost/libraryassistant";
    private String theServerUsername = "root";
    private String theServerPassword = "";

    @FXML TableView<Book> TableView_BookList;
    @FXML TableColumn<Book, String> TableColumn_Title;
    @FXML TableColumn<Book, String> TableColumn_Author;
    @FXML TableColumn<Book, String> TableColumn_Publisher;
    @FXML TableColumn<Book, Boolean> TableColumn_Available;


    ObservableList<Book> thisObservableList = FXCollections.observableArrayList();

    @Override public void initialize(URL thisURL, ResourceBundle thisResourceBundle) {
        TableColumn_Title.setCellValueFactory(new PropertyValueFactory<>("Title"));
        try {
            System.out.println("Retrieving data from database...");
            Class.forName(theConnectionDriver);
            Connection thisConnection;
            thisConnection = DriverManager.getConnection(theConnectionString, theServerUsername, theServerPassword);
            Statement thisStatement;
            thisStatement = thisConnection.createStatement();
            ResultSet thisResultSet;
            thisResultSet = thisStatement.executeQuery("SELECT * FROM `Book`");
            while(thisResultSet.next()) {
                String BookTitle =  thisResultSet.getString("Title");
                String BookAuthor =  thisResultSet.getString("Author");
                String BookPublisher =  thisResultSet.getString("Publisher");
                Boolean BookAvailable =  thisResultSet.getBoolean("Available");
                thisObservableList.add(new Book(BookTitle, BookAuthor, BookPublisher, BookAvailable));
                System.out.println(BookTitle);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        TableView_BookList.getItems().setAll(thisObservableList);
    }

    public static class Book {
        private final SimpleStringProperty BookTitle;
        private final SimpleStringProperty BookAuthor;
        private final SimpleStringProperty BookPublisher;
        private final SimpleBooleanProperty BookAvailable;

        private Book(String BookTitle, String BookAuthor, String BookPublisher, Boolean BookAvailable) {
            this.BookTitle = new SimpleStringProperty(BookTitle);
            this.BookAuthor = new SimpleStringProperty(BookAuthor);
            this.BookPublisher = new SimpleStringProperty(BookPublisher);
            this.BookAvailable = new SimpleBooleanProperty(BookAvailable);
        }

        public String getBookTitle() {
            return BookTitle.get();
        }

        public String getBookAuthor() {
            return BookAuthor.get();
        }

        public String getBookPublisher() {
            return BookPublisher.get();
        }

        public Boolean getBookAvailable() {
            return BookAvailable.get();
        }
    }
}

Full FXML:

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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="512.0" prefWidth="768.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BookList.Controller">
   <children>
      <TableView fx:id="TableView_BookList" layoutX="14.0" layoutY="256.0" prefHeight="237.0" prefWidth="479.0" AnchorPane.bottomAnchor="16.0" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="128.0">
        <columns>
          <TableColumn fx:id="TableColumn_Title" maxWidth="256.0" minWidth="256.0" prefWidth="-1.0" resizable="false" text="Title" />
          <TableColumn fx:id="TableColumn_Author" maxWidth="256.0" minWidth="256.0" prefWidth="-1.0" resizable="false" text="Author" />
            <TableColumn fx:id="TableColumn_Publisher" maxWidth="128.0" minWidth="128.0" prefWidth="-1.0" resizable="false" text="Publisher" />
            <TableColumn fx:id="TableColumn_Available" maxWidth="64.0" minWidth="64.0" prefWidth="-1.0" resizable="false" text="Available" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </children>
</AnchorPane>

System.out.println Debug Message: enter image description here

As you can see, "Legend of None, My, Ada" is the Book Title, it does show the value in console yet I have clickable rows, but it doesn't show anything (words).

Clickable rows has the same amount as the data in database. enter image description here

Lem Bidi
  • 63
  • 9

1 Answers1

0

As the code is incomplete, can't find the issue. Try debugging it whether the String values BookTitle , BookAuthor, BookPublisher and BookAvailable are getting retrieved from the database else print those values and check.