我正在尝试从SQLite数据库中的数据填充TableView,但我遇到了一个非常奇怪的情况,我无法理解是什么导致它。
tableview只填充两列,而不填充其余列。最终显示TableView时,不会填充带有'NO'和'Date Created'的Tablecolumns。
但是,此代码在“标题”和“描述”TableView列中显示来自SQLite数据库的数据。
请鹰眼的人帮助我确定我在这段代码上出错的地方。我花了一天的大部分时间试图弄清楚我哪里出错了,但我似乎并不知道我做错了什么。我很乐意感谢你的任何帮助。
这是我的
代码块引用
public class Notedb extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("ListNotesUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
块引用
块引用
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="notedb.test.ListNotesUIController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.5" layoutX="186.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="196.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Button alignment="TOP_CENTER" contentDisplay="TEXT_ONLY" layoutX="484.0" layoutY="22.0" mnemonicParsing="false" onAction="#newNote" prefHeight="54.0" prefWidth="66.0" text="New Note" textAlignment="CENTER" wrapText="true" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="126.0" layoutY="2.0" prefHeight="94.0" prefWidth="596.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="441.0" minWidth="10.0" prefWidth="441.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="292.0" minWidth="10.0" prefWidth="155.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="m_search" onAction="#searchNotes" />
<Label fx:id="labelNOs" alignment="CENTER" prefHeight="17.0" prefWidth="94.0" text="4 Notes" GridPane.columnIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="181.0" layoutY="98.0" prefHeight="196.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Pane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<Button layoutX="95.0" layoutY="24.0" mnemonicParsing="false" prefHeight="54.0" prefWidth="100.0" text="Delete" />
<Button fx:id="btn_medit" layoutX="389.0" layoutY="24.0" mnemonicParsing="false" onAction="#editNoteRow" prefHeight="54.0" prefWidth="94.0" text="Edit" />
</children>
</Pane>
<TableView id="tableNotes" fx:id="tableNotes" editable="true" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn id="noCol" fx:id="noCol" text="NO">
</TableColumn>
<TableColumn id="titleCol" fx:id="titleCol" text="Title">
</TableColumn>
<TableColumn id="dateCreatedCol" fx:id="dateCreatedCol" text="Date Created">
</TableColumn>
<TableColumn id="descriptionCol" fx:id="descriptionCol" text="Description">
</TableColumn>
</columns>
</TableView>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
块引用
块引用
public class ListNotesUIController implements Initializable {
@FXML
private Label label;
@FXML
private Label labelNOs;
@FXML
private Button newNote;
@FXML
private Button btn_medit;
@FXML
private TextField m_search;
@FXML
private TableView tableNotes;
@FXML
private TableColumn titleCol;
@FXML
private TableColumn descriptionCol;
@FXML
private TableColumn dateCreatedCol;
@FXML
private TableColumn noCol;
//START | SQLITE
private static Connection con;
private static Statement stat;
private PreparedStatement prep;
//END | SQLITE
private ObservableList <Note> dataNotes;
DataBank dbank = new DataBank();
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@FXML
private void editNoteRow(ActionEvent event) {
}
@FXML
private void newNote(ActionEvent event) throws IOException {
}
@FXML
private void searchNotes(ActionEvent event){
}
@Override
public void initialize(URL url, ResourceBundle rb) {
dataNotes = FXCollections.observableArrayList();
noCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("idno")
);
dateCreatedCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("datecreated")
);
titleCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("title")
);
descriptionCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("description")
);
try {
SQLiteConfig config = new SQLiteConfig();
con = DriverManager.getConnection("jdbc:sqlite:Note.db");
stat = con.createStatement();
stat.executeUpdate("CREATE TABLE IF NOT EXISTS NotesDB (idno INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Title VARCHAR(500), Description VARCHAR(1000), DateCreated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);");
ResultSet rs = con.createStatement().executeQuery("SELECT idno, Title, DateCreated, Description FROM NotesDB");
while (rs.next()) {
Note nt = new Note();
nt.idno.set(rs.getString("idno"));
nt.title.set(rs.getString("Title"));
nt.datecreated.set(rs.getString("DateCreated"));
nt.description.set(rs.getString("Description"));
dataNotes.add(nt);
}
tableNotes.setItems(dataNotes);
} catch (SQLException ex) {
Logger.getLogger(ListNotesUIController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
块引用
块引用
public class Note {
public SimpleStringProperty title = new SimpleStringProperty();
public SimpleStringProperty description = new SimpleStringProperty();
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
public String getTitle() {
return title.get();
}
public void setTitle(String titleStr) {
title.set(titleStr);
}
public String getDescription() {
return description.get();
}
public void setDescription(String descriptionStr) {
description.set(descriptionStr);
}
public String getDateCreated() {
return datecreated.get();
}
public void setDateCreated(String datecreatedStr) {
datecreated.set(datecreatedStr);
}
public String getIdNO() {
return idno.get();
}
public void setIdNO(String idnoStr) {
idno.set(idnoStr);
}
}
块引用
答案 0 :(得分:1)
您的财产命名存在错误。 您的getDateCreated和IdNO函数与之无关 命名惯例。
替换
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
与
public SimpleStringProperty dateCreated = new SimpleStringProperty();
public SimpleStringProperty idNO = new SimpleStringProperty();