2

This is the java

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class mains extends Stage {

public static void main(String[] args) {
    new JFXPanel();
    Platform.runLater(new Runnable(){

        @Override
        public void run() {
            new mains();
        }

    });
}
void go(){
    new JFXPanel();
    new mains().show();
}

public mains() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
            "LOL.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@FXML
private Button button;

@FXML
private Label label;

@FXML
void push(ActionEvent event) {

}

}

here is the fxml http://pastebin.com/uzBrMRDV I get a load exception it says Root is already specified. If i remove the setRoot(this); it doesnt load at all I am getting really frustrated with JFX... Is there anyway to load FXML files like a Stage from the controller itself

Ryan S.
  • 134
  • 2
  • 9

1 Answers1

7

Remove the line

fxmlLoader.setRoot(this);

Your FXML defines the root to be an AnchorPane (and you can't set the root twice, which is why you are getting the error).

Since the current class is a Stage, and the FXMLLoader loads an AnchorPane, you need to put the loaded AnchorPane in a Scene and set the scene in the stage. Replace

fxmlLoader.load();

with

AnchorPane root = fxmlLoader.load();
Scene scene = new Scene(root); // optionally specify dimensions too
this.setScene(scene);
James_D
  • 201,275
  • 16
  • 291
  • 322