Comment appeler launch() plus d'une fois en java


Comment appeler launch() plus d'une fois en java on me donne une exception comme "ERREUR DANS MAIN:java.lang.IllegalStateException: Le lancement de l'application ne doit pas être appelé plus d'une fois "

J'ai créé rest cleint dans mon application java lorsque la demande vient, il appelle javafx et ouvre webview après avoir terminé webview operarion je ferme javafx windows en utilisant Platform.méthode exit (). lors de la deuxième demande vient obtiens cette erreur comment filtrer cette erreur.

Application JavaFX Code:

public class AppWebview extends Application  {

    public static Stage stage;

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

        stage = _stage;
        StackPane root = new StackPane();

        WebView view = new WebView();

        WebEngine engine = view.getEngine();
        engine.load(PaymentServerRestAPI.BROWSER_URL);
        root.getChildren().add(view);
        engine.setJavaScriptEnabled(true);
        Scene scene = new Scene(root, 800, 600);
        stage.setScene(scene);

        engine.setOnResized(new EventHandler<WebEvent<Rectangle2D>>() {
            public void handle(WebEvent<Rectangle2D> ev) {
                Rectangle2D r = ev.getData();
                stage.setWidth(r.getWidth());
                stage.setHeight(r.getHeight());
            }
        });

        JSObject window = (JSObject) engine.executeScript("window");
        window.setMember("app", new BrowserApp());

        stage.show();

    }

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

Méthode RestClient: Appel à l'application JavaFX

// method 1 to lanch javafx
javafx.application.Application.launch(AppWebview.class);

// method 2 to lanch javafx
String[] arguments = new String[] {"123"};
AppWebview .main(arguments);
Author: venu, 2014-06-20

2 answers

Vous ne pouvez pas appeler launch() sur une application JavaFX. plus d'une fois, il n'est pas permis.

Du javadoc:

It must not be called more than once or an exception will be thrown.

Suggestion pour afficher une fenêtre périodiquement

  1. Il suffit d'appeler Application.launch() une fois.
  2. Gardez le runtime JavaFX en arrière-plan en utilisant Platform.setImplicitExit(false), de sorte que JavaFX ne s'arrête pas automatiquement lorsque vous masquez la dernière fenêtre d'application.
  3. La prochaine fois que vous aurez besoin d'une autre fenêtre, enveloppez la fenêtre show() appelez Platform.runLater(), pour que l'appel soit exécuté sur le thread d'application JavaFX.

Si vous mélangez Swing, vous pouvez utiliser un JFXPanelau lieu d'une Application, mais le modèle d'utilisation sera similaire à celui décrit ci-dessus.

Échantillon de Wumpus

import javafx.animation.PauseTransition;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.*;

// hunt the Wumpus....
public class Wumpus extends Application {
    private static final Insets SAFETY_ZONE = new Insets(10);
    private Label cowerInFear = new Label();
    private Stage mainStage;

    @Override
    public void start(final Stage stage) {
        // wumpus rulez
        mainStage = stage;
        mainStage.setAlwaysOnTop(true);

        // the wumpus doesn't leave when the last stage is hidden.
        Platform.setImplicitExit(false);

        // the savage Wumpus will attack
        // in the background when we least expect
        // (at regular intervals ;-).
        Timer timer = new Timer();
        timer.schedule(new WumpusAttack(), 0, 5_000);

        // every time we cower in fear
        // from the last savage attack
        // the wumpus will hide two seconds later.
        cowerInFear.setPadding(SAFETY_ZONE);
        cowerInFear.textProperty().addListener((observable, oldValue, newValue) -> {
            PauseTransition pause = new PauseTransition(
                    Duration.seconds(2)
            );
            pause.setOnFinished(event -> stage.hide());
            pause.play();
        });

        // when we just can't take it  anymore,
        // a simple click will quiet the Wumpus,
        // but you have to be quick...
        cowerInFear.setOnMouseClicked(event -> {
            timer.cancel();
            Platform.exit();
        });

        stage.setScene(new Scene(cowerInFear));
    }

    // it's so scary...
    public class WumpusAttack extends TimerTask {
        private String[] attacks = {
                "hugs you",
                "reads you a bedtime story",
                "sings you a lullaby",
                "puts you to sleep"
        };

        // the restaurant at the end of the universe.
        private Random random = new Random(42);

        @Override
        public void run() {
            // use runlater when we mess with the scene graph,
            // so we don't cross the streams, as that would be bad.
            Platform.runLater(() -> {
                cowerInFear.setText("The Wumpus " + nextAttack() + "!");
                mainStage.sizeToScene();
                mainStage.show();
            });
        }

        private String nextAttack() {
            return attacks[random.nextInt(attacks.length)];
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 19
Author: jewelsea, 2014-06-24 07:14:32

Essayez ceci, j'ai essayé ceci et j'ai réussi

@Override
public void start() {
    super.start();
    try {
                    // Because we need to init the JavaFX toolkit - which usually Application.launch does
                    // I'm not sure if this way of launching has any effect on anything
        new JFXPanel();

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // Your class that extends Application
                new ArtisanArmourerInterface().start(new Stage());
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 2
Author: Dahsri, 2017-03-13 13:11:40