IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaFX Discussion :

Javafx composant perso InvocationTargetException gettext()


Sujet :

JavaFX

  1. #1
    Membre averti

    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    104
    Détails du profil
    Informations personnelles :
    Âge : 63
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 104
    Points : 395
    Points
    395
    Par défaut Javafx composant perso InvocationTargetException gettext()
    Bonjour
    je débute en Java
    j'ai créé un composant dont voici le contenu du fichier fxml

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="UTF-8"?>
     
    <?import java.lang.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
     
    <fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="132.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
        <children>
            <Button id="btnCustom" fx:id="btnDea" alignment="CENTER" contentDisplay="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#doSomething" prefHeight="100.0" prefWidth="100.0" text="DeaButton" textAlignment="CENTER" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="16.0" />
            <Label id="lblCustom" fx:id="lblDea" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="100.0" text="Label" textAlignment="CENTER" wrapText="true" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="132.0" />
        </children>
    </fx:root>
    et le controller
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    package sample.gui.custom;
     
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.AnchorPane;
     
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
     
    public class CustomButton extends AnchorPane {
     
        private static final Logger logger = Logger.getLogger(CustomButton.class.getName());
        public StringProperty stringProperty = new SimpleStringProperty();
     
        @FXML
        private  Label lblCustom;
        @FXML
        private  Button btnCustom;
     
     
        public CustomButton() {
            //super();
     
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CustomButton.fxml"));
            fxmlLoader.setController(this);
            fxmlLoader.setRoot(this);
     
            this.setWidth(132);
            this.setHeight(200);
     
     
            try {
                fxmlLoader.load();
            } catch (IOException ex) {
                Logger.getLogger(CustomButton.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
     
        public String getLblCustomtext() {
            return lblCustom.getText();
        }
     
        public void setLblCustomText(String value) {
            lblCustom.setText(value);;
        }
     
        @FXML
        protected void doSomething() {
            System.out.println("The button was clicked!");
            System.out.println(getLblCustomtext());
        }
    }
    je l'utilise grâce à une application de test avec le code suivant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
    package sample;
     
    import javafx.application.Application;
    import javafx.application.Preloader;
    import javafx.beans.property.BooleanProperty;
    import javafx.beans.property.SimpleBooleanProperty;
    import javafx.concurrent.Task;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
     
    import sample.gui.custom.CustomButton;
     
     
     
    public class TestApp extends Application {
        Stage stage;
     
        public static void main(String[] args) {
            launch(args);
        }
     
     
     
        @Override
        public void start(final Stage stage) throws Exception {
            CustomButton customButton = new CustomButton();
            StackPane root = new StackPane();
            root.getChildren().add(customButton);
            stage.setScene(new Scene(root, 400, 400));
            stage.show();
        }
    }
    l'apllication fonctionne correctement mais quand jze clique sur le bouton, je reçois une execption

    The button was clicked!
    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8390)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$112/2091436502.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:927)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/774324403.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
    ... 50 more
    Caused by: java.lang.NullPointerException
    at sample.gui.custom.CustomButton.getLblCustomtext(CustomButton.java:50)
    at sample.gui.custom.CustomButton.doSomething(CustomButton.java:60)
    ... 60 more
    lblCustom dans le controler est toujours nul

    si j'essaie de definir le texte du label à la création par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    customButton.setLblCustomText("Mon texte");
    j'ai le même problème.
    l'erreur est probablement évidente.
    j'ai fouillé sur le net mais je n'arrive pas à trouver d'info.

    merci pour votre aide
    cordialement
    Lazarus 0.9.30-4 et FPC 2.4.4
    on
    debian Lenny i386
    debian Lenny amd64
    Ubuntu 10.04 i386
    Ubuntu 10.04 amd64
    MacOSX panther (10.5.8)
    Window 7 32 bits

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Pour ce que j'en vois dans le débogueur, tes éléments marqués par @FXML ne sont jamais injectés au chargement.
    Après vérification, cela vient tout simplement du fait que tes fx:id dans ton FXML ne correspondent pas aux noms des variables annotées dans le contrôle.

    Remplace :

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <Button id="btnCustom" fx:id="btnDea" ...
    <Label id="lblCustom" fx:id="lblDea" ...

    Par :

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <Button id="btnCustom" fx:id="btnCustom" ...
    <Label id="lblCustom" fx:id="lblCustom" ...

    Rappel :
    • id -> propriété id de l'objet.
    • fx:id -> nom de la variable dans le contrôleur.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre averti

    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    104
    Détails du profil
    Informations personnelles :
    Âge : 63
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 104
    Points : 395
    Points
    395
    Par défaut
    J'ai trouvé le problème



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <Button id="btnCustom" ...
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <Label id="lblCustom" ...
    au lieu de
    Lazarus 0.9.30-4 et FPC 2.4.4
    on
    debian Lenny i386
    debian Lenny amd64
    Ubuntu 10.04 i386
    Ubuntu 10.04 amd64
    MacOSX panther (10.5.8)
    Window 7 32 bits

  4. #4
    Membre averti

    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    104
    Détails du profil
    Informations personnelles :
    Âge : 63
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 104
    Points : 395
    Points
    395
    Par défaut
    @bouye

    merci

    nos messages se sont croisés
    Lazarus 0.9.30-4 et FPC 2.4.4
    on
    debian Lenny i386
    debian Lenny amd64
    Ubuntu 10.04 i386
    Ubuntu 10.04 amd64
    MacOSX panther (10.5.8)
    Window 7 32 bits

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Taille composant perso via le layout ?
    Par Neilos dans le forum Composants
    Réponses: 6
    Dernier message: 04/12/2005, 17h33
  2. [C#]Problème sur composant perso (Bouton Transparent)
    Par SLE dans le forum Windows Forms
    Réponses: 1
    Dernier message: 06/10/2005, 00h12
  3. Composant perso disparaît lors du redimensionnement du composant parent
    Par GENERYS dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 05/10/2005, 22h07
  4. Rendre un composant perso Scrollable
    Par GENERYS dans le forum Composants
    Réponses: 2
    Dernier message: 26/09/2005, 14h36
  5. Réponses: 1
    Dernier message: 08/07/2005, 02h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo