Can' fix fxml.LoadException - java

I know that many people ask about this problem, and Iread about this error and I tried to fix that, but I don't know why it's still does not work.
I got this code:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.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);
}
}
And I tried to change getResource part like this "/Login.fxml". But it still does not work.
This is how project files look:

Try changing this line
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
to
Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("application/Login.fxml"));

Since FXML is in the same path you can do something like this:
Parent root = FXMLLoader.load(Main.class.getResource("Login.fxml"));
You should verify Login.fxml is defined as a resource in the project and it's present in the output directory .

Related

How to solve JavaFX runtime components are missing on VS code IDE

I'm using JavaFx on visual studio code IDE. I always get this error:
JavaFX runtime components are missing and are required to run this application
I've already added the VM args and the javafx libraries.
Also some basic FX codes are compiled with ease, but once I use the FXMLLoader class, I get the aforementioned error.
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application
{
#Override
public void start(Stage stage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource("/interfaces/SignIn.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
In launch.json, adding a new entry for the vmArgs including the --module-path with the local path to the JavaFX SDK and --add-modules with the required JavaFX modules:
"vmArgs": "--module-path /path/to/javafx/lib --add-modules javafx.controls,javafx.fxml"

How to Fix NoClassDefFoundError in JavaFX?

I'm trying to build a small user interface using JavaFX, But I'm getting an error like this:
Error: Could not find or load main class myApp Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application
This is my code:
and im using jdk 12.0.2
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class myApp extends Application{
public static void main(String[] args) {
Phonebook mybook = new Phonebook();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 600, 300);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Phone Book");
primaryStage.setScene(scene);
primaryStage.show();
}
This is the Libraries and jdk I'm using:
Image1
I think JavaFX is not a part of the JDK > 9 any more. (Your version is 12.x.x)
Probably this could help you:
https://openjfx.io/openjfx-docs/#install-javafx
(Assuming you are using Maven) If this does not help, try to clean and build your Application.
Sometimes Maven does not recognize newly added dependencies.

javafx import cannot be resolved in eclipse

I am starting an assignment and just installed javafx from the Eclipse marketplace, after I installed and created a javafx project with fxml however in main the imports are showing errors saying "the import javafx cannot be resolved". I am new to Java and Eclipse and have tried to search for similar questions but they all seem to be different cases.
I would appreciate any help thank you.
Below is the code, and all the imports are showing errors:
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("Student.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);
}
}
I have figured that it is the library JavaFx SDK which is not able to be added to the project, is anyone good with Eclipse can help solve this problem? I tried configure build path and removing the JavaFx SDK and readding, still does not work.

How to add a style.css to my application through the scene builder in JavaFX?

I am trying to add style.css to my java application in intellij. So I have added a new directory named resources and put the style.css inside that folder. Inside my scene builder I have specified in the GridPane that IO want the file "style.css" in my resources to control the view. However, whenever I run the code I get this error.
Error:
Oct 29, 2018 3:56:58 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not find stylesheet: file:/C:/Users/Troy22/Desktop/Java/EMCGrabExtension/out/production/resources/style.css
I tried different ways, but non have seemed to help. Thanks again for your help
Code:
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 {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root,770,450);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
scene.getStylesheets().add("style.css");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Try Adding it like this in your Main:
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
You can try adding it through scenebuilder (On the top hierarchy node, so if your root node is a BorderPane than add it on this node):
However I would recommend adding it like the comment before mine with:
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
Don't forget the .toExternalForm() after the getResource() method.
And be sure your resource folder is in the classpath so Java will find it.

Error when trying to pass a variable to another controller in javafx/scenebuilder

I'm making a simple program wherein I am trying to add a string from a TextField to a ListView in another scene. The problem is I am getting a NullPointerException when I pass the string to the method which will add it to the List.
Here is the code for controller for the main scene:
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class MainPageController {
#FXML TextField txtfield;
public void addButton(ActionEvent event) {
String a=txtfield.getText();
FXMLLoader loader= new FXMLLoader();
loader.setLocation(getClass().getResource("/progra/view/ListView.fxml"));
ListController obj=loader.getController();
obj.addList(a);// <----NullPointerException
}
public void openList(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/program /view/ListView.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
}
}
Here is the code for the controller of the list scene:
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
public class ListController{
#FXML
ListView<String> listofstrs;
public void addList(String a) {
listofstrs.getItems().add(a);
}
}
What do I need to do to fix it? If I may add, is it possible to add variables to a ListView (or any other text containers) in another stage/scene without opening the stage/scene where it is contained, and when you open it you will see the things you added? If so, what do i need to add?
Because you are calling the getController() method, and the controller is not set within the Java source, you must have the controller defined within your fxml file. Can you post the fxml source?
In your fxml, your root element should have the fx:controller attribute set to the class path of the controller, e.g:
fx:controller="com.class.path.to.ListController

Categories