Issue
I'm learning JavaFX and I'm trying to make a graphical app with SceneBuilder, but the window is blank with nothing on it. I added some buttons to the window on SceneBuilder, but I can't see them when I run the app. The app is based on MVC:
Main:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
@Override
public void start(Stage stage) {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("Test.fxml"));
AnchorPane root = (AnchorPane) loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
public class Controller {
@FXML
private AnchorPane base;
@FXML
public void setBackgroundRed(ActionEvent event) {
base.setStyle("-fx-background-color: red;");
}
@FXML
public void setBackgroundGreen(ActionEvent event) {
base.setStyle("-fx-background-color: green;");
}
}
That's what I made on SceneBuilder, and what the GUI looks like: SceneBuilder GUI
That's what I get instead, when I run the application: blank window
Edit: if I close and reopen SceneBuilder the GUI is ok. image
Solution
You're using Eclipse IDE right?
I think the issue is Eclipse. In fact, Eclipse by default doesn't refersh project resources automatically. Therefore, if you update and save your .fxml file from SceneBuilder, it goes out-of-sync inside the IDE, until you refresh the resources (right click on Project folder > Refresh, or F5).
You can make Eclipse refresh resources automatically by selecting the option Refresh using native hooks or polling: Window > Preferences > General > Workspace > "refresh resources automatically".
That issue had been widely discussed in this thread.
Answered By - mikyll98
Answer Checked By - Candace Johnson (JavaFixing Volunteer)