【问题标题】:JavaFx Table View hidden the String value [duplicate]JavaFx表视图隐藏了字符串值[重复]
【发布时间】:2017-06-04 08:12:16
【问题描述】:

我的 TableView 不显示任何字符串,但它有 Clickable(存在,但不显示任何文字)。

使用的数据库: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 调试信息

如您所见,“Legend of None, My, Ada”是书名,它确实在控制台中显示了值,但我有可点击的行,但它没有显示任何内容(单词)。

可点击的行与数据库中的数据具有相同的数量。

【问题讨论】:

  • 您的PropertyValueFactory 调用与实际的属性名称不匹配。查看标记的重复问题。
  • 对不起,我不知道我应该使用哪个关键字。
  • 您是否阅读了链接问题的答案?它解释得很透彻。

标签: java mysql javafx


【解决方案1】:

由于代码不完整,找不到问题。尝试调试它是否正在从数据库中检索字符串值BookTitle , BookAuthor, BookPublisher and BookAvailable,否则打印这些值并检查。

【讨论】:

  • 调试消息确实显示了该值,但不知道为什么它没有显示在表格上。
  • 刷新,我已经编辑了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多