I'm using OpenJDK11.
I have two Java files in the current folder, that are supposed to run together to be a JavaFX application.
One of them is called Main.java and runs the main window. Another is Alert.java, and is supposed to run an alternate window which is an alert type.
Now, I ran the following command:
javac -cp "c:\projects\java\currentProject" --module-path "c:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml Alert.java Main.java
While Alert.java compiled just fine, Main.java could not import the Alert class and gave an error on "import Alert". I tried "import Alert.Alert" and "import currentProject.Alert" but still, it didn't work.
Also, I declared package "package currentProject" at the start of each file and it still gave an error.
What am I supposed to do to get it running? I already failed on installing JavaFX on all available IDEs, so I'm not going to use an IDE other than Atom. But how do I compile it properly?
more info -
file structure:
c->projects->java->economicManager->( Alert.java ,Main.java, financialManager.fxml, alert.fxml, Alert.class, Alert$Controller.class, Main.class [previously compiled version])
Alert.java:
package financialManager;
import javafx.stage.Stage;
import javafx.stage.Modality;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import java.util.Map;
public class Alert {
public Stage stage;
private Controler_Class controler;
public Alert(Parent root) {
Controler_Class clas = new Controler_Class(root);
this.controler = clas;
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("financial report");
stage.setScene(scene);
this.stage = stage;
stage.showAndWait();
}
private class Controler_Class{
Parent root;
public Controler_Class(Parent root){
}
}
}
Main.java:
package financialManager;
import Alert;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import java.util.Map;
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception{
final int width = 300;
final int height = 450;
stage.setTitle("hello mofos");
FXMLLoader loader = new FXMLLoader(getClass().getResource("financialManager.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, width, height);
Map<String, Object> mapper = loader.getNamespace();
AnchorPane pane = (AnchorPane) mapper.get("splitpane1_anchorpane");
if(pane != null)
SplitPane.setResizableWithParent(pane, false);
else
System.out.println("it's null you idiot!");
Button btn = (Button) mapper.get("economicReport");
btn.setOnMouseClicked((event) -> {
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("alert.fxml"));
Parent parent = loader2.load();
Alert alert = new Alert(parent);
});
/*
ChangeListener<Number> stageSizeListener = (observable, oldValue, newValue) ->
pane.setDividerPositions(0.20219435736677116);
stage.widthProperty().addListener(stageSizeListener);
stage.heightProperty().addListener(stageSizeListener);
*/
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch();
}
}
I see that you are importing the Alert class wrongly. Your package is financialManager, so you should use it in the import line like this:
import financialManager.Alert;
About your issues with IDEs, I made JavaFX work fine with Eclipse and IntelliJ on OpenJDK 11 without any issues a few days ago - For OpenJDK you will need OpenJFX, and if you are interesting in some reading, this is the link from Oracle's blog on their plans for JavaFX.
Good luck!
Related
This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
I have two packages p1 and p2.
P1 contains the main class, controller class and one fxml file.
P2 contains the controller class and one fxml file.
I want to switch from p1 fxml to p2 fxml file.
here is the code I tried. this is in P1 package.
public void btncontinue(ActionEvent event)throws IOException {
String filepath = "file:///D:/Programs/InteliJProjects/C/src/p1/sample2.fxml";
Parent nextScene = FXMLLoader.load(getClass().getClassLoader().getResource(filepath));
Scene scene = new Scene(nextScene);
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.show();
}
The error i am getting is Location is required.
Well I've checked this is the code from scene1 to scene2
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
public class sample
{
#FXML
RadioButton male;
#FXML
AnchorPane rootPane;
public void add() throws IOException
{
AnchorPane pane = FXMLLoader.load(getClass().getResource("two.fxml"));
rootPane.getChildren().setAll(pane);
}
}
And this is how to return from scene2 to scene1
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
public class Two
{
#FXML
Button back;
#FXML
AnchorPane secPane;
public void returnBack() throws IOException
{
AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
secPane.getChildren().setAll(pane);
}
}
I've tried this and it is working fine hope it will help you
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 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 tried to run the following code in my Netbeans 7.2 (Java 1.7.u79) and Netbeans 8.0.2 (Java 1.8 u45) and its not working!
package com.main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class RBrowser extends Application {
#Override
public void start(Stage primaryStage) {
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
webEngine.load("http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm");
Button b = new Button("Show Console");
b.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent t) {
webEngine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
}
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(browser,b);
Scene scene = new Scene(root, 700, 550);
primaryStage.setTitle("Google Map");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I tried to access http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm in the Google Chrome Browser and it was loaded successfully, WHAT'S WRONG WITH MY CODE?
Which version of JDK have you installed. I am using IDK 8u77 on Windows 8.1 and running on Eclipse Mars2 your code, it worked perfectly. Try latest version from here. Check it works or not.
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);
}