Solved Load JavaFX Scene Controller from FXML

Discussion in 'Plugin Development' started by MCMastery, Jun 19, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    MCMastery

    Hi! I am trying to use a JavaFX scene with my plugin. I can load it fine, and it opens, but once I attach a controller to it, I get an error that says the controller class cannot be found:
    https://pastebin.com/QreF50Ji

    Here is my MCServer class: (I made it the controller AND the JavaPlugin just to prove it should be able to find it. I've tried it also with a separate controller class but same problem):

    Code:
    public class MCServer extends JavaPlugin implements Initializable {
        @Override
        public void onEnable() {
            new Thread(() -> ControlPanel.launch(ControlPanel.class)).start();
        }
    
        @FXML
        private TextArea console;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            this.console.appendText("TEST");
        }
    
        public void sendCommand(ActionEvent evt) {
            TextField textField = (TextField) evt.getSource();
            textField.setText("");
            String cmd = textField.getText();
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
        }
    }
    ControlPanel:
    Code:
    public class ControlPanel extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("control-panel.fxml"));
            primaryStage.setTitle("MCServer Control Panel");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    }
    Everything is in the same package (com.dgrissom.mcserver).

    Maybe the JavaFX reflection stuff doesn't like Bukkit?
    I really want to be able to use FXML, creating the scene through Java is a pain.

    Thanks!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MCMastery Correct me if I am wrong, but isn't javafx used to display stuff?
    Why do you want to use it?
     
  3. Offline

    MCMastery

    I want to make a control panel for my server.
    I was able to do this using Swing, but I want to see if it is possible using JavaFX
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    MCMastery

    @timtower thanks for help, but actually @IAlInstannen helped me solve this in the Discord server.

    The problem is with Bukkit's class loader likely, it doesn't like the JavaFX reflection stuff to get the controller class.

    I got around this by setting the controller manually:
    Code:
    FXMLLoader loader = new FXMLLoader(getClass().getResource("control-panel.fxml"));
    loader.setController(new ControlPanelController());
    Parent root = loader.load();
    The IDE will say a bunch of errors in the FXML file since there's no controller specified and there's method calls, but it still compiles thankfully.
     
Thread Status:
Not open for further replies.

Share This Page