My JavaFX project structure is:
src
- main
- java
- Second
- Second.java
- SecondView.java
- SecondViewModel.java
- First.java
- FirstView.java
- FirstViewModel.java
...
- resources
- fxml
- First.fxml
- Second.fxml
I got a getter
-function for the primarystage
in my First.java
file. When I try to access it from SecondView.java
I'm getting the error Can't resolve symbol
.
When I move the SecondView.java
into the java
root package, so that they're just on the same level, there is no problem at all.
First.java
import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.sql.SQLException;
public class First extends Application {
private static Logger LOG = LogManager.getLogger(First.class);
private static Stage primaryStage;
public static Stage getPrimaryStage() {
return primaryStage;
}
public static void setPrimaryStage(Stage oStage) {
this.primaryStage = oStage;
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws SQLException, IOException, ClassNotFoundException {
FirstView.initMainWindow();
}
}
SecondView.java
package Second;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sun.applet.Main;
import util.DatabaseHandler;
import util.I18N;
import util.StageHandler;
import util.Util;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class SecondView {
private static Logger LOG = LogManager.getLogger(SecondView.class);
public static void initSecondWindow() throws IOException, SQLException, ClassNotFoundException {
LOG.debug("initSecondWindow start...");
Stage primaryStage = First.getPrimaryStage();
ResourceBundle bundle = ResourceBundle.getBundle("i18n.myproject", I18N.getLocale());
FXMLLoader fxmlLoader = new FXMLLoader(SecondView.class.getResource("fxml/Second.fxml"), bundle);
Parent parentRoot = fxmlLoader.load();
Scene rootScene = new Scene(parentRoot);
primaryStage.setScene(rootScene);
primaryStage.setTitle(Util.getConfValue("conf/general.properties", "application.title"));
primaryStage.show();
StageHandler.addStage("gui_Second", primaryStage);
DatabaseHandler.dbConnect();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
StageHandler.delStage("gui_Second");
}
});
LOG.debug("...finished.");
}
}
Update Solution provided by @Jamed_D in his comment and linking to the possible duplicate.