Partage
  • Partager sur Facebook
  • Partager sur Twitter

JavaFX, Problème pour lancer mon application

    24 septembre 2024 à 9:18:16

    Bonjour, je débute JavaFX, et j'ai eu un problème lorsque j'ai voulu lancer mon programme, je pense que c'est un problème lié à « SimpleStringProperty », car j'ai initialiser des variables avec ce type dans "Locataire.java" et je crois que JavaFX n'arrive pas à reconnaitre ce type (qui est pourtant un type fait pour JavaFX). Si quelqu'un a des idées pour résoudre ce problème, cela m'aiderait à comprendre :)
    Voici les codes de mon application : 
    (pour information la scène principale n'est pas encore codée, le problème vient donc de la seconde fenêtre)
    Main.java : 
    package application;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
            primaryStage.setTitle("Application avec plusieurs fenêtres");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    Controller.Java :
    package application;
    
    import java.io.IOException;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    import javafx.stage.Stage;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.fxml.FXMLLoader;
    
    public class Controller {
        @FXML
        private Button openSecondWindowButton;
    
        @FXML
        public void initialize() {
        }
    
        @FXML
        private void openSecondWindow() throws IOException  {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("second_window.fxml"));
                Parent root = loader.load();
                Stage secondStage = new Stage();
                secondStage.setTitle("Deuxième fenêtre");
                secondStage.setScene(new Scene(root));
                secondStage.show();
        }
    }
    SecondWindowController.java : 
    package application;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    import javafx.stage.Stage;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextField;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.input.MouseEvent;
    
    public class SecondWindowController {
    	//zone de texte
    	@FXML
    	private TextField id_locataire;
    
    	@FXML
    	private TextField nom_locataire;
    
    	@FXML
    	private TextField prenom_locataire;
    
    	@FXML
    	private TextField email_locataire;
    
    	@FXML
    	private TextField telephone_locataire;
    
    	//bouton
    	@FXML
    	private Button ajoute_ligne_tab_locataire;
    
    	@FXML
    	private Button supprime_ligne_tab_locataire;
        @FXML
        private Button close_fen_tab_locataire;
        
        //tableau
        @FXML
        private TableView<Locataire> table_locataire;
        
        @FXML
        private TableColumn<Locataire, String> id_locataire_tab;
    
        @FXML
        private TableColumn<Locataire, String> nom_locataire_tab;
    
        @FXML
        private TableColumn<Locataire, String> prenom_locataire_tab;
    
        @FXML
        private TableColumn<Locataire, String> email_locataire_tab;
    
        @FXML
        private TableColumn<Locataire, String> telephone_locataire_tab;
    
        private ObservableList<Locataire> locataires = FXCollections.observableArrayList();
    
        @FXML
        private void initialize() {
            id_locataire_tab.setCellValueFactory(new PropertyValueFactory<>("id")); 
            nom_locataire_tab.setCellValueFactory(new PropertyValueFactory<>("nom")); 
            prenom_locataire_tab.setCellValueFactory(new PropertyValueFactory<>("prenom")); 
            email_locataire_tab.setCellValueFactory(new PropertyValueFactory<>("email")); 
            telephone_locataire_tab.setCellValueFactory(new PropertyValueFactory<>("telephone"));
    
            table_locataire.setItems(locataires);
        }
    
        @FXML
        private void closeWindow() {
            Stage stage = (Stage) close_fen_tab_locataire.getScene().getWindow();
            stage.close();
        }
        
        @FXML
        private void ajouteLigneTabLocataire(MouseEvent event) {
            locataires.add(new Locataire(
                id_locataire.getText(),
                nom_locataire.getText(),
                prenom_locataire.getText(),
                email_locataire.getText(),
                telephone_locataire.getText()
            ));
    
            id_locataire.clear();
            nom_locataire.clear();
            prenom_locataire.clear();
            email_locataire.clear();
            telephone_locataire.clear();
        }
        
        @FXML
        private void supprimeLigneTabLocataire(MouseEvent event) {
            Locataire selectedLocataire = table_locataire.getSelectionModel().getSelectedItem();
            if (selectedLocataire != null) {
                locataires.remove(selectedLocataire);
            }
        }    
    }
    Locataire.java :
    package application;
    
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    public class Locataire {
        private final SimpleStringProperty id;
        private final SimpleStringProperty nom;
        private final SimpleStringProperty prenom;
        private final SimpleStringProperty email;
        private final SimpleStringProperty telephone;
    
        public Locataire(String id, String nom, String prenom, String email, String telephone) {
            this.id = new SimpleStringProperty(id);
            this.nom = new SimpleStringProperty(nom);
            this.prenom = new SimpleStringProperty(prenom);
            this.email = new SimpleStringProperty(email);
            this.telephone = new SimpleStringProperty(telephone);
        }
    
        public String getId() {
            return id.get();
        }
    
        public void setId(String id) {
            this.id.set(id);
        }
    
        public String getNom() {
            return nom.get();
        }
    
        public void setNom(String nom) {
            this.nom.set(nom);
        }
    
        public String getPrenom() {
            return prenom.get();
        }
    
        public void setPrenom(String prenom) {
            this.prenom.set(prenom);
        }
    
        public String getEmail() {
            return email.get();
        }
    
        public void setEmail(String email) {
            this.email.set(email);
        }
    
        public String getTelephone() {
            return telephone.get();
        }
    
        public void setTelephone(String telephone) {
            this.telephone.set(telephone);
        }
    
        public StringProperty idProperty() {
            return id;
        }
    
        public StringProperty nomProperty() {
            return nom;
        }
    
        public StringProperty prenomProperty() {
            return prenom;
        }
    
        public StringProperty emailProperty() {
            return email;
        }
    
        public StringProperty telephoneProperty() {
            return telephone;
        }
    }
    Main.fxml : 
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>
    
    <AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="application.Controller">
        <Button fx:id="openSecondWindowButton" text="Ouvrir la deuxième fenêtre" onAction="#openSecondWindow" layoutX="50.0" layoutY="50.0"/>
    </AnchorPane>
    second_window.fxml : 
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.text.Font?>
    
    <BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SecondWindowController">
       <center>
          <TableView fx:id="table_locataire" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <columns>
              <TableColumn fx:id="id_locataire_tab" prefWidth="56.0" text="Id" />
              <TableColumn fx:id="nom_locataire_tab" prefWidth="123.20001220703125" text="Nom" />
                <TableColumn fx:id="prenom_locataire_tab" prefWidth="123.2000732421875" text="Prénom" />
                <TableColumn fx:id="email_locataire_tab" prefWidth="190.39996337890625" text="Email" />
                <TableColumn fx:id="telephone_locataire_tab" prefWidth="109.60003662109375" text="Téléphone" />
            </columns>
          </TableView>
       </center>
       <bottom>
          <VBox alignment="BOTTOM_CENTER" prefHeight="154.0" prefWidth="600.0" BorderPane.alignment="CENTER">
             <children>
                <HBox prefHeight="54.0" prefWidth="600.0">
                   <children>
                      <Label prefHeight="18.0" prefWidth="18.0" text="Id :">
                         <HBox.margin>
                            <Insets bottom="10.0" left="20.0" right="5.0" top="10.0" />
                         </HBox.margin>
                      </Label>
                      <TextField fx:id="id_locataire" prefHeight="26.0" prefWidth="43.0">
                         <HBox.margin>
                            <Insets top="7.0" />
                         </HBox.margin>
                         <font>
                            <Font size="13.0" />
                         </font>
                      </TextField>
                      <Label prefHeight="18.0" prefWidth="34.0" text="Nom :">
                         <HBox.margin>
                            <Insets bottom="10.0" left="50.0" right="5.0" top="10.0" />
                         </HBox.margin>
                      </Label>
                      <TextField fx:id="nom_locataire" prefHeight="26.0" prefWidth="150.0">
                         <HBox.margin>
                            <Insets top="7.0" />
                         </HBox.margin>
                         <font>
                            <Font size="13.0" />
                         </font>
                      </TextField>
                      <Label prefHeight="18.0" prefWidth="50.0" text="Prénom :">
                         <HBox.margin>
                            <Insets bottom="10.0" left="50.0" right="5.0" top="10.0" />
                         </HBox.margin>
                      </Label>
                      <TextField fx:id="prenom_locataire" prefHeight="26.0" prefWidth="145.0">
                         <HBox.margin>
                            <Insets top="7.0" />
                         </HBox.margin>
                         <font>
                            <Font size="13.0" />
                         </font>
                      </TextField>
                   </children>
                </HBox>
                <HBox prefHeight="52.0" prefWidth="600.0">
                   <children>
                      <Label prefHeight="18.0" prefWidth="36.0" text="Email :">
                         <HBox.margin>
                            <Insets bottom="10.0" left="20.0" right="5.0" top="10.0" />
                         </HBox.margin>
                      </Label>
                      <TextField fx:id="email_locataire" prefHeight="26.0" prefWidth="187.0">
                         <HBox.margin>
                            <Insets top="7.0" />
                         </HBox.margin>
                         <font>
                            <Font size="13.0" />
                         </font>
                      </TextField>
                      <Label prefHeight="18.0" prefWidth="65.0" text="Téléphone :">
                         <HBox.margin>
                            <Insets bottom="10.0" left="50.0" right="5.0" top="10.0" />
                         </HBox.margin>
                      </Label>
                      <TextField fx:id="telephone_locataire" prefHeight="26.0" prefWidth="135.0">
                         <HBox.margin>
                            <Insets top="7.0" />
                         </HBox.margin>
                         <font>
                            <Font size="13.0" />
                         </font>
                      </TextField>
                   </children>
                </HBox>
                <HBox alignment="CENTER" prefHeight="33.0" prefWidth="600.0">
                   <children>
                      <Button fx:id="ajoute_ligne_tab_locataire" mnemonicParsing="false" text="Ajoute une ligne" onMouseClick="#ajouteLigneTabLocataire">
                         <HBox.margin>
                            <Insets bottom="10.0" left="10.0" right="20.0" />
                         </HBox.margin>
                      </Button>
                      <Button fx:id="supprime_ligne_tab_locataire" mnemonicParsing="false" text="Supprimer la ligne" onMouseClick="#supprimeLigneTabLocataire">
                         <HBox.margin>
                            <Insets bottom="10.0" right="290.0" />
                         </HBox.margin>
                      </Button>
                      <Button fx:id="close_fen_tab_locataire" mnemonicParsing="false" text="Fermer" textAlignment="JUSTIFY">
                         <HBox.margin>
                            <Insets bottom="10.0" right="10.0" />
                         </HBox.margin>
                      </Button>
                   </children>
                </HBox>
             </children>
          </VBox>
       </bottom>
    </BorderPane>
    Et voici les erreurs que j'ai eu : 
    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1858)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1726)
    at javafx.base@21.0.4/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base@21.0.4/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
    at javafx.base@21.0.4/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
    at javafx.base@21.0.4/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@21.0.4/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base@21.0.4/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@21.0.4/javafx.scene.Node.fireEvent(Node.java:8875)
    at javafx.controls@21.0.4/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls@21.0.4/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
    at javafx.controls@21.0.4/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base@21.0.4/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base@21.0.4/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base@21.0.4/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
    at javafx.base@21.0.4/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
    at javafx.base@21.0.4/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@21.0.4/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@21.0.4/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@21.0.4/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base@21.0.4/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@21.0.4/javafx.scene.Scene$MouseHandler.process(Scene.java:3984)
    at javafx.graphics@21.0.4/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
    at javafx.graphics@21.0.4/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2708)
    at javafx.graphics@21.0.4/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics@21.0.4/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@21.0.4/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics@21.0.4/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics@21.0.4/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics@21.0.4/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    at javafx.graphics@21.0.4/com.sun.glass.ui.View.notifyMouse(View.java:937)
    at javafx.graphics@21.0.4/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@21.0.4/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:833)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:72)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.base@21.0.4/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:270)
    at javafx.fxml@21.0.4/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1855)
    ... 46 more
    Caused by: javafx.fxml.LoadException: 
    /C:/Users/loris/eclipse-workspace/MultiWindows/bin/application/second_window.fxml:108
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2722)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2700)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
    at application.Controller.openSecondWindow(Controller.java:23)
    ... 58 more
    Caused by: java.lang.UnsupportedOperationException: Cannot determine type for property.
    at javafx.fxml@21.0.4/com.sun.javafx.fxml.BeanAdapter.getSetterMethod(BeanAdapter.java:178)
    at javafx.fxml@21.0.4/com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:251)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:631)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:783)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2969)
    at javafx.fxml@21.0.4/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2654)
    ... 61 more
    Merci pour votre aide :)

    -
    Edité par LorisCaro 24 septembre 2024 à 9:21:00

    • Partager sur Facebook
    • Partager sur Twitter
      28 mars 2025 à 15:54:54 - Message modéré pour le motif suivant : Message complètement hors sujet


      JavaFX, Problème pour lancer mon application

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      • Editeur
      • Markdown