简体   繁体   中英

my loader class can't load the .fxml file

I want to built a library management system in which i want to add the book into my database which is working perfectly.But when I want to retrive data from database my loader class can't load my fxml file This is the structure of my project enter image description here

This is my code Book_listController.java

package library.listbook;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
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 library.database.DatabaseHandler;

public class Book_listController implements Initializable {

    ObservableList<Book> list = FXCollections.observableArrayList();
    @FXML
    private TableView<Book> tableview;
    @FXML
    private TableColumn<Book, String> titlecol;

    @FXML
    private TableColumn<Book, String> idcol;

    @FXML
    private TableColumn<Book, String> availabilitycol;

    @FXML
    private TableColumn<Book, String> authorcol;

    @FXML
    private TableColumn<Book, String> publishercol;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        iniCol();
       try {
           loadData();
       } catch (SQLException ex) {
           Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex);
       }
    } 
    private void iniCol(){
    titlecol.setCellValueFactory(new PropertyValueFactory<>("title"));
    idcol.setCellValueFactory(new PropertyValueFactory<>("id")); 
    authorcol.setCellValueFactory(new PropertyValueFactory<>("author"));
    publishercol.setCellValueFactory(new PropertyValueFactory<>("publisher"));
    availabilitycol.setCellValueFactory(new PropertyValueFactory<>("available"));
    }
    private void loadData() throws SQLException{
    DatabaseHandler handler=new DatabaseHandler();
    String qu="select * from book";
    ResultSet resultset=handler.exeQuery(qu);
    try{
    while(resultset.next()){
    String id=resultset.getString("id");
    String title=resultset.getString("title");
    String author=resultset.getString("author");
    String publisher=resultset.getString("publisher");
    String avail=resultset.getString("isAvail");

    list.add(new Book(id,title,author,publisher,avail));
    }
    }catch(SQLException ex){
    Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex);
    }
    tableview.getItems().setAll(list);
    }
    public static class Book{
    private final SimpleStringProperty title;
    private final SimpleStringProperty id;
    private final SimpleStringProperty author;
    private final SimpleStringProperty publisher;
    private final SimpleStringProperty available;
    Book(String title,String id,String author,String pub,String avail){
    this.title=new SimpleStringProperty(title);
    this.id=new SimpleStringProperty(id);
    this.author=new SimpleStringProperty(author);
    this.publisher=new SimpleStringProperty(pub);
    this.available=new SimpleStringProperty(avail);
    }
        public String getTitle() {
            return title.get();
        }

        public String getId() {
            return id.get();
        }

        public String getAuthor() {
            return author.get();
        }

        public String getPublisher() {
            return publisher.get();
        }

        public String getAvailable() {
            return available.get();
        }




    }
}

Book_listloader.java `

package library.listbook;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Dell
 */
public class Book_listloader extends Application  {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("book_list.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

And This is my fxml file book_list.fxml `

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="library.listbook.Book_listController">
    <stylesheets>
        <URL value="@book_list.css" />
    </stylesheets>
   <children>
      <TableView fx:id="tableview" layoutX="37.0" layoutY="14.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="titlecol" prefWidth="125.0" text="Book Title" />
          <TableColumn fx:id="idcol" minWidth="2.0" prefWidth="111.0" text="Book id" />
            <TableColumn fx:id="authorcol" prefWidth="110.0" text="Author" />
            <TableColumn fx:id="publishercol" prefWidth="141.0" text="Publisher" />
            <TableColumn fx:id="availabilitycol" prefWidth="112.0" text="Availability" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
    `

Your file book_list.fxml is inside the folder library.listbook , but when you call getClass().getResource it find resources inside resources folder. Let's try to modify the path to your file. Ex: library.listbook\\book_list.fxml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM