JDK doesn't see application and other classes - java

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/

Related

Why can't I run Main.java on Eclipse "Errors exist in required project(s)"

Here is error when I press run:
Down is Main.java and I don't know what this is. Yesterday I used this file and nothing happened.
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Scene;
import javafx.scene.image.Image;
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("LogIn.fxml"));
Scene scene = new Scene(root, 700,512);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setTitle("Log In");
Image icon = new Image(getClass().getResourceAsStream("sherr.jpeg"));
primaryStage.getIcons().add(icon);
primaryStage.setResizable(false);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Here is what displayed on Console

JavaFX, New window won't run

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").toExtern‌​alForm());

Full-screen Overlay

Currently, I am trying to create a timer that displays in the corner of your screen. I want it to show even if there is currently a full-screened application running. Currently I've tried Stage#setAlwaysOnTop(true), however that only functions for normal applications, not when they are full-screened.
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 stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Timer");
stage.setScene(new Scene(root, 300, 275));
stage.setAlwaysOnTop(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the standard JavaFX example with IntelliJ, however with the one modification which was Stage#setAlwaysOnTop(true). How would I get this to function ontop of the full-screen, or at least stay omnipresent no matter the application.

Eclipse doesn't want to run simple JavaFX application

I have other classes named Test in other packages and one class with the same name in the default package.
When I click the Run button in Eclipse, instead of running this class, it runs another Test class from inside another package instead:
package jfx;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Test extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
}
How can I fix this?
Add a main method to allow Eclipse recognize the program as runnable application
public static void main(String[] args) {
Application.launch(args);
}

How do you add to an existing scene in javafx?

I would like to add more javafx objects to my scene but i am not sure how. I have tried looking it up but i could not find anything.
package application;
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 {
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
Scene scene = new Scene(root,600,400);
// how would i add something here or further on?
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Test");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
For example how would i add a polygon to this?
You don't add them to scene, but to root, the Parent node of the scene. You have to change Parent to whatever type of node you're using in the FXML file. The default in netbeans is AnchorPane so that's what I used.
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane root = FXMLLoader.load(getClass().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 600, 400);
//how would i add something here or further on?
root.getChildren().add(new Polygon(10,20,30,10,20,30));
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Test");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
// don't leave me hanging bro!
Platform.exit();
}
}
I would recommend a different approach entirely. If you are using the NetBeans IDE, you can download a tool called SceneBuilder. This application lets you build and edit simple or complex JavaFX applications.
As your application becomes more and more complex, it makes more sense to use a tool like SceneBuilder. I used SceneBuilder to create a fairly complex client GUI in less than an hour. Life in JavaFX is easier with NetBeans and SceneBuilder (and I'm a guy who prefers Eclipse!)

Categories