Reputation: 11
I've not been writing for a long time. Let's go directly to the point.
I have a weird issue with a JavaFX application I wrote. I started as beginner in java, my work is finished and is not needed to compile it, but since this is important as well as programming I gave it a try.
I developed my application using eclipse and e(fx)clipse, but since I was unable to compile it with eclipse I used IntelliJ 2016.1.1(64) for this pourpose.
The issue regars the swtiching (or loading, or whatever you want to call it) between fxml, and obviously related controllers. All this code run on eclipe or intelliJ and works perfectly, but with the compiled application it simply does nothing!
This is the code, first I have a util class called UILoader.java which can be found in the util package, its pourpose is to completely switch the stage or to add a window into the existing stage by using 2 different methods, switchView and addWindow :
package GodSoft.ing.icdta.CarLoan.util;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class UILoader {
/**
* this method adds a new window in modality mode
* @param event
* @param fxmlURL
* @param windowTitle
*/
public void addWindow(ActionEvent event, String fxmlURL, String windowTitle){
Stage modalityStage = new Stage();
modalityStage.setTitle(windowTitle);
modalityStage.setResizable(false);
System.out.println("newXMLParent -> " + fxmlURL );
Parent newXMLParent = null;
try {
newXMLParent = FXMLLoader.load( getClass().getResource("../"+fxmlURL) ) ;
} catch (IOException e) {
e.printStackTrace();
}
Scene newXMLScene = new Scene(newXMLParent);
Image image = new Image( getClass().getResource("../img/corner.png").toExternalForm() );
modalityStage.setScene(newXMLScene);
modalityStage.initModality(Modality.WINDOW_MODAL);
modalityStage.initStyle(StageStyle.UNIFIED);
modalityStage.initOwner(((Node) event.getSource()).getScene().getWindow());
modalityStage.getIcons().setAll( image );
modalityStage.show();
}
/**
* This method provides the switch between scenes
* @param event
* @param fxmlURL
* @param windowTitle
*/
public void switchView(ActionEvent event, String fxmlURL, String windowTitle){
Parent newXMLParent = null;
try {
newXMLParent = FXMLLoader.load( getClass().getResource("../"+fxmlURL) );
} catch (IOException e) {
e.printStackTrace();
}
Scene newXMLScene = new Scene(newXMLParent);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(newXMLScene);
stage.setTitle(windowTitle);
stage.setResizable(false);
stage.show();
}
}
Then this class is called in many fxml controllers to switch view or add a window, this is an example of both:
This switches the view (used to go back to the "father" view) :
void CloseContractDoGoBack(ActionEvent event) {
UILoader switcher = new UILoader();
switcher.switchView(event, "fxml/ContractsHandler.fxml", "Car Loan");
}
While this is used to add a window:
void CarLoginDoInfo(ActionEvent event) {
UILoader switcher = new UILoader();
switcher.addWindow(event, "fxml/Info.fxml", "About Car Loan");
}
As you can see the code is really simple.
More information about how the app is organized, there's the main package called CarLoan where you can find the main.java class, and then some other packages, all the fxml are contained in the fxml children package, the dao in the dao package, the controller in the controller package and so on.
I want to underline this row in the UILoader class, duplicaded into its 2 methods:
newXMLParent = FXMLLoader.load( getClass().getResource("../"+fxmlURL) ) ;
As you can see I first go out the package and then come back in, I needed to write this because when I moved my UILoader class into the util package I got null pointer exception about not finding fxml resources... so basically the code becomes eg. :
newXMLParent = FXMLLoader.load( getClass().getResource("../fxml/mainWindow.fxml" ) ;
When I launch the COMPILED application it works (its logic I mean, I am sure), but the interface loader does not at all! For example, the first interface got a info button, by clicking it, it should load an About pop-up window, but it does nothing! When I insert correct login data it should log-in and load another view, but it does nothing! And I'm sure that the logic into the controller works...I really don't know what to do, I searched the web for hours, I found absolutely nothing about this issue. Hope someone can help me...
If you need the configuration used in IntelliJ just tell me which file to copy, since there are even eclipse .xml files in the project tree...Basically it's the standard javaFX application configuration of intelliJ, I found out that intelliJ has a bug when compiling exe, to make it compile the exe you need to build all configurations, and that's what I have done. I've located the main class and linked the external libraries (mysql connector) needed to make the program work, that's all!
Thank you all for your patience
I'm working on JDK1.8
UPDATE: I was able to start the program with console messages (I had issue with the jdk path for some reason screwed up), what I got when a fxml is requested is "Null pointer exception: Location is required".
But I still don't understand why...
UPDATE:
I tried every possible solution combining "../", "/" and simply the fxml url. I even tried to use
File f = new File(fxmlURL);
String filePath = f.getAbsolutePath();
...
newXMLParent = FXMLLoader.load( getClass().getResource( filePath ) ) ;
Nothing worked...I checked the compiled jar, it has the same tree posted in the screen of my project, the fxml files are located into the fxml folder into the jar, I run out of ideas
Upvotes: 0
Views: 177
Reputation: 11
Thanks to a friend, I found out the solution.
I had to give the absolute complete path of the project, it now works both in the ide and the compiled version of my app.
The solution follows:
public class UILoader {
private String gb = "/GodSoft/ing/icdta/CarLoan/";
...
try {
newXMLParent = FXMLLoader.load( getClass().getResource( gb + fxmlURL ) ) ;
...
}
If anyone wants to enhance this solution, is welcome!
Thanks to everyone for its time!
Upvotes: 0