I found this piece of code, but it won't run a new blank window and keep getting NullPointerException error. p.s. I'm new to programming. Any help would be appreciated thanks.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
BorderPane root = new BorderPane();
try {
Scene scene = new Scene(root,640,480);
scene.getStylesheets().add(getClass().getResource("/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
root.setCenter(new RootLayout());
}
public static void main(String[] args) {
launch(args);
}
}
I had the same common problem when I started with JavaFX but I can explain It,
It throws null pointer exception because it is not able to find your CSS file from the specified location.
I've found that you are getting nullpointer exception at the below line,
scene.getStylesheets().add(getClass().getResource("/application.css").toExternalForm());
There is also another way to add your CSS file to scene
1) scene.getStylesheets().add("application.css");
2) scene.getStylesheets().add(this.getClass().getResource("/application.css").toString());
3) Package should be inside src directory and css also should be in src directory.
scene.getStylesheets().add(<packageName>.<ClassName>.class.getResource("/application.css").toExternalForm());
Related
i am having some trouble including images in my JavaFX project. The project stores the resources in the src/main/resources folder and it houses the FXML documents and all the images. The FXML documents load just fine, but the image locations all show null.
My code is as follows...
package VennDiagram;
import java.io.File;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class View extends Application{
public static Stage primaryStage;
public static Scene promptWindow;
public static Scene refactor;
public static Scene scene;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception{
System.out.println(getClass().getResource("/views/View.fxml"));
System.out.println(getClass().getResource("/views/openingPage.fxml"));
System.out.println(getClass().getClassLoader().getResource("/views/openingPage.css"));
primaryStage = stage;
System.out.println(getClass().getResourceAsStream("/views/newFileButton.png"));
Parent root = FXMLLoader.load(getClass().getResource("/views/View.fxml"));
Parent root2 = FXMLLoader.load(getClass().getResource("/views/openingPage.fxml"));
scene = new Scene(root);
promptWindow = new Scene(root2,1020,580);
primaryStage.setOnCloseRequest(event ->{
quitProgramAlert.display("Confirm Exit", "Are you sure you want to exit?");
if(!quitProgramAlert.closePressed) {
event.consume();
}
});
primaryStage.setMinHeight(600);
primaryStage.setMinWidth(750);
primaryStage.setTitle("VennDiagram Creator");
primaryStage.setScene(promptWindow);
primaryStage.setResizable(false);
primaryStage.show();
}
}
It gives me an error saying the resource cannot be found.
Additional Details:
The project is built using gradle. I load my FXML files in using the same method as i do for the images.
It works just fine with the the FXML documents.
EDIT: The project structure is shown below...
I'm trying to open my jar file which I exported from IntelliJ-IDEA but the jar file unfortunately doesn't open and no error happens, although the app is running on IntelliJ, I dont know if the problem is being from the jar itself or from the code ! here is the main screen code:
package controllers;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("../views/main_screen.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
primaryStage.setTitle("Salesman Manager");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The imports are there, but compiler says Application, FXMLLoader don't exist. I have configured for JRE and JDK 11, using Eclipse with JavaFX extension.
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("Apps.fxml"));
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
As Fabian already said in his comment, starting with JDK11 JavaFX is not included in the JDK anymore. You can download a separate SDK for JavaFX here: http://jdk.java.net/openjfx/
I am trying to create a program to teach people about GNU/Linux and the command line, I have my main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setTitle("Learnix");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
}
And the controller to go with it.
package sample;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import java.io.IOException;
public class loginController {
public Button loginBtn;
public void loginBtnClick() throws IOException {
System.out.println("You are logged in");
}
}
I have tried things such as:
FXMLLoader.load(getClass().getResource("lessons.fxml"));
But I can't figure out how to get it to swap scenes. I have seen many tutorials on YouTube and it Stack Overflow but many of them have all of the JavaFX on the main.java and not in separate files as I am using scenebuilder.
Thank you.
You can either call Stage.setScene() to change the whole scene or just substitute a root to the new one by Scene.setRoot():
Parent newRoot = FXMLLoader.load(getClass().getResource("lessons.fxml"));
primaryStage.getScene().setRoot(newRoot);
I am trying to get a button to print out Java is fun after being clicked and I keep getting a Cannot find symbol error when I run it. I'm not sure whether it is my import statements or an actual error in the code itself. I was wondering if someone could help me figure out why I'm getting the error?
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ButtonHandler extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Pane pane = new Pane();
Button btOK = new Button("OK");
pane.getChildren().add(btOK);
btOK.setOnAction((ActionEvent e) -> {
System.out.println("Java is Fun");
});
// Create a scene and place it in the stage
Scene scene = new Scene(Pane);
primaryStage.setTitle("Button Demo"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}