从SQLite中的数据填充JavaFX中的TableView

从SQLite中的数据填充JavaFX中的TableView,java,sqlite,javafx,tableview,tablecolumn,Java,Sqlite,Javafx,Tableview,Tablecolumn,我试图从SQLite数据库中的数据填充TableView,但我遇到了一个非常奇怪的场景,我无法理解是什么导致了它 tableview只填充两列,不填充其余列。最终显示TableView时,不会填充带有“NO”和“Date Created”的Tablecolumns 但是,此代码在“Title”和“Description”TableView列中显示来自SQLite数据库的数据 请有鹰眼的人帮我找出我在这个代码上的错误。我花了一天的大部分时间试图找出我哪里做错了,但我似乎没有弄清楚我做得不对是什么。

我试图从SQLite数据库中的数据填充TableView,但我遇到了一个非常奇怪的场景,我无法理解是什么导致了它

tableview只填充两列,不填充其余列。最终显示TableView时,不会填充带有“NO”和“Date Created”的Tablecolumns

但是,此代码在“Title”和“Description”TableView列中显示来自SQLite数据库的数据

请有鹰眼的人帮我找出我在这个代码上的错误。我花了一天的大部分时间试图找出我哪里做错了,但我似乎没有弄清楚我做得不对是什么。我将非常感谢在这方面的任何帮助

这是我的密码

  • 主类
  • 大宗报价

    大宗报价

  • FXML
  • 大宗报价

    大宗报价


    您的财产命名有错误。 getDateCreated和IdNO的函数与 命名约定

    替换

    public  SimpleStringProperty datecreated = new SimpleStringProperty();
    public  SimpleStringProperty idno = new SimpleStringProperty();
    


    看一下命名

    非常感谢@Inge指出我的命名约定是问题的原因。我还更改了以下内容:;dateCreatedCol.setCellValueFactory(//new PropertyValueFactory(“datecreated”)//错误的new PropertyValueFactory(“datecreated”)//正确;还有这一点;noCol.setCellValueFactory(//new PropertyValueFactory(“idno”)//错误的new PropertyValueFactory(“idno”)//正确;现在它工作得很好。非常感谢。
    <?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);
        }
    }
    
    public  SimpleStringProperty datecreated = new SimpleStringProperty();
    public  SimpleStringProperty idno = new SimpleStringProperty();
    
    public  SimpleStringProperty dateCreated = new SimpleStringProperty();
    public  SimpleStringProperty idNO = new SimpleStringProperty();