Modifier le Formulaire de Démarrage de l'application JavaFX


Mon projet actuel est une application JavaFX avec RMI. Actuellement, j'ai deux trois classes, que je voudrais réduire à deux, mais je suis incapable de trouver comment.

Les trois classes: StartClient.Java BannerController.Java AEXBanner.java

Codes et errormessage:

Java.lang.refléter.InvocationTargetException

Au soleil.refléter.NativeMethodAccessorImpl.invoke0 (Méthode native) à soleil.refléter.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62) à soleil.refléter.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43) à java.lang.refléter.Méthode.invoke(la Méthode.java:497) à COM.soleil.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java: 389) à COM.soleil.javafx.application.LauncherImpl.Application de lancement(LauncherImpl.java: 328) au coucher du soleil.refléter.NativeMethodAccessorImpl.invoke0 (Méthode native) à soleil.refléter.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62) à soleil.refléter.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43) à java.lang.refléter.Méthode.invoke(la Méthode.java:497) à soleil.lanceur.LauncherHelper$FXHelper.principale(LauncherHelper.java: 767) Causé par: java.lang.IllegalStateException: Pas sur le thread d'application FX; currentThread = main au com.soleil.javafx.les savoirs traditionnels.Boîte à outils.checkFxUserThread(boîte à outils.java:236) à com. sun. javafx. tk. quantum.QuantumToolkit. checkFxUserThread(QuantumToolkit. java: 423) à javafx.étape.Étape.(Étape.java: 241) à javafx.étape.Étape.(Étape.java: 227) au client.BannerController.(BannerController.java: 71) au client.BannerController.principale(BannerController.java: 174) ... 11 plus Exception exécutant le client d'application.BannerController D:\Dropbox\HBO TIC\Leerjaar 2 \ GSO3 \ AEXBanner Semaine 1 \ nbproject\build-impl.xml:1051: L'erreur suivante s'est produite lors l'exécution de cette ligne: D:\Dropbox\HBO TIC\Leerjaar 2 \ GSO3 \ AEXBanner Semaine 1 \ nbproject\build-impl.xml:805: Java retourné: 1 LA construction A ÉCHOUÉ (temps total: 17 secondes)

Au démarrage du programme avec StartClient (pendant que le serveur RMI est en cours d'exécution), tout fonctionne bien. Mais lorsque je démarre directement l'application avec la classe BannerController, Netbeans me donne l'erreur publiée ci-dessus.

Je suis sûr à 100% que le serveur RMI fonctionne bien, donc pas besoin de s'inquiéter à ce sujet.

Les trois classes peuvent être trouvées ci-dessous:

StartClient:

package client;

import javafx.application.Application;
import javafx.stage.Stage;

/**
 *
 * @author Max
 */
public class StartClient extends Application {

    @Override
    public void start(Stage primaryStage) {
        BannerController.main(null);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

BannerController: client de paquet;

/**
 *
 * @author Max
 */
import shared.IEffectenBeurs;
import shared.IFonds;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.stage.Stage;

public class BannerController extends Application {

    private AEXBanner banner;
    private IEffectenBeurs effectenbeurs;
    private Timer pollingTimer;
    private String bindingName = "Effectenbeurs";
    private Registry registry = null;
    private static boolean locateRegistry = true;

    public BannerController(String ip, int port) {
        banner = new AEXBanner();
        pollingTimer = new Timer();

        System.out.println("Client: IP Address: " + ip);
        System.out.println("Client: Port number " + port);

        if (locateRegistry) {
            // Locate registry at IP address and port number
            registry = locateRegistry(ip, port);

            // Print result locating registry
            if (registry != null) {
                printContentsRegistry();
                System.out.println("Client: Registry located");
            } else {
                System.out.println("Client: Cannot locate registry");
                System.out.println("Client: Registry is null pointer");
            }
            if (registry != null) {
                effectenbeurs = bindEffectenbeursUsingRegistry();
            }
        } else {
            // Bind fonds using Naming
            effectenbeurs = bindEffectenbeursUsingNaming(ip, port);
        }
        if (effectenbeurs != null) {
            System.out.println("Client: Effectenbeurs bound");
        } else {
            System.out.println("Client: Effectenbeurs is null pointer");
        }
        banner.start(new Stage());
        pollingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    StringBuilder sb = new StringBuilder();
                    for (IFonds f : effectenbeurs.getKoersen()) {
                        sb.append(f.toString() + " ");
                    }
                    System.out.println(sb.toString());
                    banner.setKoersen(sb.toString());
                } catch (RemoteException ex) {
                    Logger.getLogger(BannerController.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }, 0, 2000);

    }

    // Print contents of registry
    private void printContentsRegistry() {
        try {
            String[] listOfNames = registry.list();
            System.out.println("Client: list of names bound in registry:");
            if (listOfNames.length != 0) {
                for (String s : registry.list()) {
                    System.out.println(s);
                }
            } else {
                System.out.println("Client: list of names bound in registry is empty");
            }
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot show list of names bound in registry");
            System.out.println("Client: RemoteException: " + ex.getMessage());
        }
    }

    private IEffectenBeurs bindEffectenbeursUsingRegistry() {
        IEffectenBeurs effectenbeurs1 = null;
        try {
            Object object = registry.lookup(bindingName);
            effectenbeurs1 = (IEffectenBeurs) registry.lookup(bindingName);
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            effectenbeurs1 = null;
        } catch (NotBoundException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: NotBoundException: " + ex.getMessage());
            effectenbeurs1 = null;
        }
        return effectenbeurs1;
    }

    private Registry locateRegistry(String ipAddress, int portNumber) {
        Registry registry = null;
        try {
            registry = LocateRegistry.getRegistry(ipAddress, portNumber);
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot locate registry");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            registry = null;
        }
        return registry;
    }

    private IEffectenBeurs bindEffectenbeursUsingNaming(String ipAddress, int portNumber) {
        IEffectenBeurs Effectenbeurs = null;
        try {
            Effectenbeurs = (IEffectenBeurs) Naming.lookup("rmi://" + ipAddress + ":" + portNumber + "/" + bindingName);
        } catch (MalformedURLException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: MalformedURLException: " + ex.getMessage());
            Effectenbeurs = null;
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            Effectenbeurs = null;
        } catch (NotBoundException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: NotBoundException: " + ex.getMessage());
            Effectenbeurs = null;
        }
        return Effectenbeurs;
    }

    public static void main(String[] args) {
        if (locateRegistry) {
            System.out.println("CLIENT USING LOCATE REGISTRY");
        } else {
            System.out.println("CLIENT USING NAMING");
        }

//        // Get ip address of server
        Scanner input = new Scanner(System.in);
        System.out.print("Client: Enter IP address of server: ");
        String ipAddress = input.nextLine();

        // Get port number
        System.out.print("Client: Enter port number: ");
        int portNumber = input.nextInt();
        // Create client
        BannerController client = new BannerController(ipAddress, portNumber);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

    }
}

Je doute fortement que la troisième classe ait quelque chose à voir avec ce problème, mais juste au cas où: je l'ai inclus.

AEXBanner: client de paquet;

import java.rmi.RemoteException;
import javafx.animation.AnimationTimer;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class AEXBanner extends Application {

    public static final int WIDTH = 1000;
    public static final int HEIGHT = 100;
    public static final int NANO_TICKS = 20000000;
    // FRAME_RATE = 1000000000/NANO_TICKS = 50;

    private Text text;
    private double textLength;
    private double textPosition;
    private BannerController controller;

    private AnimationTimer animationTimer;
    private Timeline timeline;
    private int TIMERTICK = 200;
    private int frame = 0;
    private String koersen;

    public void setController(BannerController banner) {
        //controller = banner(this);
    }
    @Override
    public void start(Stage primaryStage) {

        Font font = new Font("Arial", HEIGHT);
        text = new Text();
        //controller = new BannerController(this);

        text.setFont(font);
        text.setFill(Color.BLACK);

        Pane root = new Pane();
        root.getChildren().add(text);
        Scene scene = new Scene(root, WIDTH, HEIGHT);

        primaryStage.setTitle("AEX banner");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.toFront();

        timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(false);

        // Start animation: text moves from right to left
        animationTimer = new AnimationTimer() {
            private long prevUpdate;

            @Override
            public void handle(long now) {
                long lag = now - prevUpdate;
                if (lag >= NANO_TICKS) {
                    // calculate new location of text
                    // TODO
                    if (textPosition < 0 - textLength) {
                        textPosition = WIDTH;
                    }
                    textPosition -= 5;

                    text.relocate(textPosition, 0);
                    prevUpdate = now;
                }
            }

            @Override
            public void start() {
                prevUpdate = System.nanoTime();
                textPosition = WIDTH;
                text.relocate(textPosition, 0);
                //setKoersen("Nothing to display");
                super.start();
            }
        };
        animationTimer.start();
    }

    public void setKoersen(String koersen) {

        text.setText(koersen);

        textLength = text.getLayoutBounds().getWidth();
    }
}

Donc pour conclure avec ma question (juste pour être clair): lorsque le serveur RMI est en cours d'exécution, je dois démarrer ce projet avec le StartClient. Le StartClient lance le BannerController. J'aimerais pouvoir démarrer le programme directement avec la classe BannerController.

Tous les conseils ou solutions possibles seraient grandement appréciés!

MODIFIER: Après le changement suggéré par Lasagna, cette erreur apparaît directement lorsque je démarre le BannerController:

Exception in Application constructor
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class client.BannerController
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: client.BannerController.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application client.BannerController
D:\Dropbox\HBO ICT\Leerjaar 2\GSO3\AEXBanner Week 1\nbproject\build-impl.xml:1051: The following error occurred while executing this line:
D:\Dropbox\HBO ICT\Leerjaar 2\GSO3\AEXBanner Week 1\nbproject\build-impl.xml:805: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Author: Joris Borza, 2016-04-11

1 answers

Jetons un coup d'œil à votre erreur.

Soleil.lanceur.LauncherHelper$FXHelper.principale(LauncherHelper.java:767) Causé par: java.lang.IllegalStateException: Pas sur le thread d'application FX; currentThread = main

Cela signifie que vous n'avez pas de "Thread d'application FX" et que votre thread actuel est "main."

C'est parce que vous essayez de travailler avec la méthode principale comme "méthode principale" mais dans FX, cela ne fonctionne pas comme ça (funky hein? :) ). principal est juste un méthode pour lancer l'application FX elle-même, c'est pourquoi vous obtenez cette erreur.


La méthode de départ de votre Application est l'endroit où les choses se passent.

Dans votre première classe "StartClient", vous utilisez le principal de "BannerController", puis dans votre principal, vous "launch(args)", c'est ainsi que vous lancez votre application FX.

Dans votre application de bannière, vous avez une classe principale avec un tas de choses, mais pas launch(args)

Je ne sais pas non plus pourquoi "start" lance une exception sans code à l'intérieur de lui.


Donc ce que vous devez faire est

  1. Supprimer l'application de toutes les classes sauf la classe principale.
  2. assurez-vous que la classe principale a "launch(args)" dans la méthode main.
  3. Assurez-vous que la classe principale remplace la méthode "start" et que vous "lancez" votre code à partir de là.

Alors, prenez ce code

public static void main(String[] args) {
        if (locateRegistry) {
            System.out.println("CLIENT USING LOCATE REGISTRY");
        } else {
            System.out.println("CLIENT USING NAMING");
        }

//        // Get ip address of server
        Scanner input = new Scanner(System.in);
        System.out.print("Client: Enter IP address of server: ");
        String ipAddress = input.nextLine();

        // Get port number
        System.out.print("Client: Enter port number: ");
        int portNumber = input.nextInt();
        // Create client
        BannerController client = new BannerController(ipAddress, portNumber);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

    }

Et le changer en

public static void main(String[] args) {
       launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
 if (locateRegistry) {
            System.out.println("CLIENT USING LOCATE REGISTRY");
        } else {
            System.out.println("CLIENT USING NAMING");
        }

//        // Get ip address of server
        Scanner input = new Scanner(System.in);
        System.out.print("Client: Enter IP address of server: ");
        String ipAddress = input.nextLine();

        // Get port number
        System.out.print("Client: Enter port number: ");
        int portNumber = input.nextInt();
        // Create client
        BannerController client = new BannerController(ipAddress, portNumber);
    }
 1
Author: XaolingBao, 2016-04-11 21:20:35