Je veux construire un système de gestion de la bibliothèque dans laquelle je veux ajouter le livre dans ma base de données qui travaille perfectly.But quand je veux retrive les données de base de données ma classe de chargeur ne peut pas charger mon fichier FXML Ceci est la structure de mon projet enter image description herema classe de chargeur ne peut pas charger le fichier .fxml
Ceci est mon 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);
}
}
Et Ceci est mon fichier FXML 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>
`
[Try this] (http://stackoverflow.com/a/43934700/4310386) –