Why doesn't my javafx stage want to load

This is 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 fxml http://pastebin.com/uzBrMRDV I get a boot exception, it says Root is already listed. If I remove setRoot (this); it doesn't load at all I'm very upset by JFX ... In any case, load FXML files such as Stage from the controller itself

+1
source share
1 answer

Delete line

fxmlLoader.setRoot(this);

Your FXML defines the root as AnchorPane(and you cannot set root twice, so you get an error).

Since the current class is Stage, and is FXMLLoaderloading AnchorPane, you need to put the loaded AnchorPanein Sceneand set the scene on stage. Replace

fxmlLoader.load();

with

AnchorPane root = fxmlLoader.load();
Scene scene = new Scene(root); // optionally specify dimensions too
this.setScene(scene);
+5

All Articles