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 thread e


Sujet :

JavaFX

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Février 2014
    Messages
    80
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2014
    Messages : 80
    Points : 46
    Points
    46
    Par défaut JavaFx thread e
    Bonsoir à tous,


    j'ai un petit problème si quelqu'un peut m'aider s'il vous plait merci.
    Voici ce que j'aimerai faire:

    j'ai mon JavaFxthread qui créer ma fênetre et qui lance un second thread, j'aimerai ensuite que se second thread appelle une fonction qui rajoute un bouton dans ma fênetre.

    En gros je veux que mon second thread(creer par mon thread principal javaFx thread) puisse appeller des fonctions qui rajoute des composants dans ma fenetre.


    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    package testjavafxthread;
     
    import java.io.IOException;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.concurrent.Task;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
     
    /**
     *
     */
    public class TestJavaFxThread extends Application {
     
     
          BorderPane pane = new BorderPane();
     
        @Override
        public void start(Stage primaryStage) {
     
     
            threadJavafx();
     
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
     
                @Override
                public void handle(ActionEvent event) {
     
     
                }
            });
     
     
            Group group = new Group();
            HBox hbox = new HBox();
            //BorderPane pane = new BorderPane();
     
            hbox.getChildren().add(btn);
            group.getChildren().add(hbox);
            pane.setCenter(group); 
     
     
            Scene scene = new Scene(pane, 300, 250);
     
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
     
        public void a()  {
            Button bouton = new Button();
            Group grp = new Group();
            grp.getChildren().add(bouton);
            pane.setLeft(grp);
     
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
     
     
     
         public void threadJavafx() {
     
              Task longTask = new Task<Void>() {
                @Override
                protected Void call() throws Exception {                             
     
                    a();
                    return null;
                }
            };           
           new Thread(longTask).start();     
        }
    Le souci c'est que j'obtiens pas d'erreur.C'est comme si mon second thread ne peut pas appeller de fonction qui "modifie ma fenetre"

  2. #2
    Membre habitué
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2010
    Messages
    212
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2010
    Messages : 212
    Points : 184
    Points
    184
    Par défaut
    aucun souci pour moi. Il fonctionne correctement sur ma machine (1.8.0_60).

  3. #3
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 838
    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 838
    Points : 22 846
    Points
    22 846
    Billets dans le blog
    51
    Par défaut
    Ceci dit pour être correct il faudrait faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    protected Void call() throws Exception {                             
       Platform.runLater(TestJavaFxThread.this::a);
       return null;
    }
    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

  4. #4
    Membre du Club
    Homme Profil pro
    Inscrit en
    Février 2014
    Messages
    80
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2014
    Messages : 80
    Points : 46
    Points
    46
    Par défaut
    Bonjour,

    excusez-moi je crois que vous m'avez mal compris, le code était sencé afficher 2 boutons un par le premier thread javafx application et le 2ème par le nouveau thread créer.

    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
     
     
    package testjavafxthread;
     
    import java.io.IOException;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.concurrent.Task;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage; 
     
    /**
     *
     */
     
    public class TestJavaFxThread extends Application {
     
     
        BorderPane root;
     
        @Override
        public void start(Stage primaryStage) {
     
            System.out.println("nom du premmier thread javagx : "+ Thread.currentThread().getName());
            threadJavafx();
     
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
     
                @Override
                public void handle(ActionEvent event) {              
     
                }
            });
     
     
            //root = new StackPane();
           //root.getChildren().add(btn);
     
           root = new BorderPane();
           root.setCenter(btn);
     
           Scene scene = new Scene(root, 300, 250);
     
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
     
        public void a()  {
     
            Button b = new Button("iii");
     
            root.setLeft(b);
     
     
     
        } 
     
        /**
         * @param args the command line arguments
         */
     
        public static void main(String[] args) {
            launch(args);
        }
     
        public void threadJavafx() {
              Task longTask = new Task<Void>() {
                @Override
                protected Void call() throws Exception {                             
                    System.out.println("nom du nouveau thread : "+ Thread.currentThread().getName());
                    a();
                    return null;
                }
            };           
           new Thread(longTask).start();     
        }
     
     
     
    }
    il est censé afficher 2 boutons le premier bouton("Hello World!") par le threadJavaFx application et le 2eme bouton("iii") par le nouveau thread créer, là il y que le premier bouton(hello word) qui s'affiche pourtant la méthode a() est bien executer par le second thread

  5. #5
    Membre habitué
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2010
    Messages
    212
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2010
    Messages : 212
    Points : 184
    Points
    184
    Par défaut
    Je pense que vous n'avez pas lit la réponse de bouye.

    remplacer:
    par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     Platform.runLater(()-> a());

Discussions similaires

  1. [Thread] JavaFX et Thread
    Par dada-06 dans le forum JavaFX
    Réponses: 4
    Dernier message: 09/04/2018, 05h50
  2. Réponses: 1
    Dernier message: 09/10/2014, 01h58
  3. thread java et javafx
    Par chungech dans le forum JavaFX
    Réponses: 2
    Dernier message: 24/03/2010, 11h56
  4. Tri multi-threadé
    Par Tifauv' dans le forum C
    Réponses: 8
    Dernier message: 28/06/2007, 10h00
  5. [Kylix] Pb de Thread !!
    Par Anonymous dans le forum EDI
    Réponses: 1
    Dernier message: 25/04/2002, 14h53

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