JavaFX Stage wont show in Android

388 views Asked by At

I have deployed a JavaFX application in Android using Gluon Plugin. What Im trying to do right now is to show a second Stage, but unfortunately it wont show.

Here's the code of the second Stage:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Setting {

    public Setting(){
        Text title = new Text("Settings");
        title.setId("setting-title");
        title.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.04)));

        Label label_url = new Label("URL");
        label_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        Label label_style = new Label("Style");
        label_style.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        TextField text_url = new TextField(MainApp.URL);
        text_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));

        ComboBox style_box = new ComboBox();
        style_box.setStyle("-fx-font-size:"+(int)(MainApp.HEIGHT * 0.03)+"px;");

        ObservableList<String> data = FXCollections.observableArrayList();
        data.add("Blue");
        data.add("Red");
        data.add("Yellow");
        style_box.setItems(data);

        GridPane grid = new GridPane();
        grid.setVgap(5);
        grid.setHgap(5);
        grid.add(label_url, 0, 0);
        grid.add(label_style, 0, 1);
        grid.add(text_url, 1, 0);
        grid.add(style_box, 1, 1);

        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(title,grid);

        Scene scene = new Scene(root, MainApp.WIDTH * 0.6, MainApp.HEIGHT * 0.4);

        if(MainApp.SETTING == null){
            MainApp.SETTING = new Stage();
            MainApp.SETTING.setScene(scene);
            MainApp.SETTING.initOwner(MainApp.MAINSTAGE);
            MainApp.SETTING.setTitle("Settings");
        }
    }

    public void show(){
        if(!MainApp.SETTING.isShowing()) MainApp.SETTING.show();
    }

 }

This code works when I tried to run it as a desktop application. Im using Android 4.2.

1

There are 1 answers

0
José Pereda On BEST ANSWER

Using JavaFXPorts you can change scenes and stages while running on Android.

I've modified the Gluon plugin for NetBeans default sample to achieve this, first switching scenes:

private Scene mainScene;

@Override
public void start(Stage stage) {
    Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

    Label label = new Label("Main Scene");        
    StackPane root = new StackPane(label);
    mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

    stage.setScene(mainScene);
    stage.show();

    label.setOnMouseClicked(e->{
        Label labelSettings = new Label("Settings Scene. Click to go back");
        StackPane rootSettings = new StackPane(labelSettings);
        Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
        stage.setScene(settingsScene);
        labelSettings.setOnMouseClicked(t->stage.setScene(mainScene));
    });
}

Now switching stages:

private Scene mainScene;

@Override
public void start(Stage stage) {
    Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

    Label label = new Label("Main Stage");        
    StackPane root = new StackPane(label);
    root.setStyle("-fx-background-color: aquamarine;");
    mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());

    stage.setScene(mainScene);
    stage.setTitle("Main Stage");
    stage.show();

    label.setOnMouseClicked(e->{
        Label labelSettings = new Label("Settings Stage. Click to go back");
        StackPane rootSettings = new StackPane(labelSettings);
        rootSettings.setStyle("-fx-background-color: burlywood;");
        Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
        Stage stage2 = new Stage();
        stage2.setScene(settingsScene);
        stage2.show();
        labelSettings.setOnMouseClicked(t->stage2.hide());
    });
}

And there's another possibility: you can use Gluon Charm, to manage different views.

Have a look at this project to get you started.