-1

This is a snippet of my code when I was using Swing:

private static Gui gui;

public static void main(String[] args){
    gui = new Gui(); //(1)
    gui.menu();      //(2)
    gui.show();      //(3)
}
  1. Constructor initialises JFrame
  2. Creates JLabel and JPanel and adds them both into frame.
  3. Set frame visibility to true.

I've been trying but I can't get the result I want using Java-FX. This is how my last attempt looked:

private static Gui gui;

public static void main(String[] args){
    gui = new Gui();
    Application.launch(Gui.class, args);
    gui.menu();
    gui.show();
}

Gui class:

private static Stage stage;

public class Gui extends Application {

    publc void start(Stage primaryStage){

    Gui.stage = primaryStage;
    stage.setTitle("title");

    //I read that some method blocking is involved here, I want to go back!
    }

    public void menu(){

    BorderPane abc = new BorderPane();
    Scene scene = new Scene(abc, 200, 200);
    stage.setScene(scene);

    //if I somehow mange to reach this part, there will be some null pointer
    //exception, well everything looks real initialised to me
    }

    public void show(){

    stage.show();

    //I have never reach this part
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3157517
  • 21
  • 2
  • 8
  • 3
    As an aside. The Swing code is wrong in that it fails to start the GUI on the EDT. A further concern with the code is that it declared the `gui` member as `static`. **Don't** try to replicate it in any other GUI toolkit. Instead start with [Getting Started with JavaFX](http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm) & learn how to use the GUI Toolkit correctly (something you should have *also* done with Swing). – Andrew Thompson Jul 04 '16 at 12:07
  • 2
    I'm voting to close this question as off-topic because the asker ***should*** begin with [Getting Started with JavaFX](http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm). – Andrew Thompson Jul 04 '16 at 12:10
  • Don't worry, I finally figure out the proper way it should be done. It was surprisingly easy. Anyway maybe i should delete this – user3157517 Jul 04 '16 at 13:30
  • You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188). – trashgod Jul 04 '16 at 15:22

1 Answers1

1

The simplest way is using Application.launch(_:_:) as follows :

package gui;

import javafx.application.Application;


public class NewClass {
    public static void main(String[] args) {
        Application.launch(Gui.class, args);
    }
}

– and have your class like this:

package gui;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Gui extends Application {
    private Scene scene;
    private Stage stage;
    
    @Override
    public void start(Stage stage) {
        this.stage = stage;
        Button btn = new Button("test");
        this.scene = new Scene(btn, 200, 250);
        stage.setTitle("title");
        stage.setScene(scene);
        stage.show();
    }
    
    public void menu() {
        BorderPane abc = new BorderPane();
        this.scene = new Scene(abc, 200, 200);
        this.stage.setScene(scene);

        // If I somehow manage to reach this part, there will be a NullPointerException
        // Everything looks real initialised to me
    }
}

There are more samples here of what you are trying to achieve.

D3181
  • 2,037
  • 5
  • 19
  • 44