0

How can I embed vert.x http server inside JavaFX application so I can start/stop the http server via JavaFX GUI?

1
  • Vert.x is just a library so you can create an instance inside any Java app and deploy verticles. Does that answer your question? Commented Jun 11, 2018 at 14:50

1 Answer 1

1

You should simply set a classic EventHandler on the Button.

The EventHandler body should start an HttpServer following Vert.x semantics.

A sample code should be as follows:

import io.vertx.reactivex.core.Vertx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXRunner extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        Button btn = new Button("Start server");
        btn.setOnAction(event -> {
            Vertx.vertx().createHttpServer()
                    .requestHandler(req -> req.response().end("Bingo :D"))
                    .listen(8080);
            btn.setDisable(true);
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, 300, 250);
        stage.setTitle("Vert.x Server");
        stage.setScene(scene);
        stage.show();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.