2017-06-04 171 views
0

我的TableView不顯示任何字符串,但它有可點擊(存在,但沒有顯示任何文字)。JavaFx表視圖隱藏字符串值

使用的數據庫:MySQL。

完全控制器

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

完全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 調試消息enter image description here

正如你所看到的,「樂無,我的Ada「是書名,它確實顯示了控制檯中的值,但我有可點擊的行,但它沒有顯示任何內容(單詞)。

可點擊的行與數據庫中的數據具有相同的數量。 enter image description here

+0

您的'PropertyValueFactory'調用與實際屬性名稱不匹配。請參閱標記爲重複的問題。 – Itai

+0

抱歉,我不知道我應該使用哪個關鍵字。 –

+1

你讀過關聯問題的答案嗎?它很徹底地解釋它。 – Itai

回答

0

由於代碼不完整,無法找到問題。嘗試調試它是否從數據庫檢索字符串值BookTitle , BookAuthor, BookPublisher and BookAvailable否則打印這些值並檢查。

+0

調試消息顯示值,但不知道爲什麼它不顯示在表上。 –

+0

刷新,我編輯了我的問題。 –