G
G
Gfd2015-12-19 23:26:00
Java
Gfd, 2015-12-19 23:26:00

How to load FXML file in static method?

I understand that the question may seem stupid, but still. I need to somehow make this code work. I know that you can't statically reference a non-static getClass() method
public class Exercise{
static Parent panel1;
public static void start() throws Exception {
Stage WindowExercise=new Stage();
panel1=FXMLLoader.load(getClass().getResource("src//application//Window1(Otgimanie).fxml"));
Scene scene1=new Scene(panel1,600,270);
WindowExercise.setScene(scene1);
WindowExercise.show();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
moryakov, 2015-12-24
@moryakov

maybe something like that?

public class Exercise {
  /*skipped fields*/
  public void loadScene() {
    Stage WindowExercise=new Stage();
    panel1=FXMLLoader.load(getClass().getResource("src//application//Window1(Otgimanie).fxml"));
    Scene scene1=new Scene(panel1,600,270); 
    WindowExercise.setScene(scene1);
    WindowExercise.show();
  }
  public static void main(String[] args) {
    Excercise ex = new Exercise();
     ex.loadScene();
  }
}

A
akimdi, 2016-01-06
@akimdi

most likely so...

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("путь до файла *.fxml"));
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question