Exception in Application constructor while making a constructor - javafx

When I am running the below mentioned code it is working
import javafx.application.Application;
public class Client {
public static void main(String[] args){
Test t2 = new Test();
Application.launch(t2.getClass(),args);
}
}
where the test class is
package com.temp.com.serverclient;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
But if I am trying to add constructor,it is getting Exception in Application constructor,Error.
The code is
package com.temp.com.serverclient;
import javafx.application.Application;
public class Client {
public static void main(String[] args){
Test t1 = new Test("Pass this String to Constructor");
Application.launch(t1.getClass(),args);
}
}
Test class
package com.temp.com.serverclient;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
String str;
public Test(String str) {
this.str = str;
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
How Can I sovle the problem? I need to pass the String to collect information from previous class.

Application.launch always uses a public parameterless constructor to create a instance of the application class to launch. (It does not provide any benefit to create a instance in the main method BTW. Simply pass the class without creating a instance, i.e. Application.launch(Test.class, args);.)
In fact you can only pass String parameters to the new instance of your application class without using static members and it's done via the args parameter of Application.launch:
public class Client {
public static void main(String[] args) {
Application.launch(Test.class, "Pass this String to Constructor");
}
}
public class Test extends Application {
String str;
#Override
public init() {
this.str = getParameters().getRaw().get(0);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("No Main");
StackPane root = new StackPane();
root.getChildren().add(new Label("It worked!"));
primaryStage.setScene(new Scene(root, 300, 120));
primaryStage.show();
}
}
Note that accessing the parameters property is also possible for the start method.
JavaFX 9 introduced a new possiblility: using Platform.startup but you need to handle the lifecycle of the application class yourself:
Application app = new Test("Pass this String to Constructor");
app.init();
Platform.startup(() -> {
Stage stage = new Stage();
try {
app.start(stage);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
});
This does not properly call the Application.stop method though. Also the parameters are not assigned.

AFAIK, Application.launch creates a new instance of Test. Therefore, you need another way to get the value to the instance, e. g. using a static getter method in your Client class which is called from Test

Related

JavaFX multiple scene opening, duplicating of scene

I want to have a login page (which I have working), that then goes to a new scene (which I dont have working yet).
I then want this scene to have items that you can click on. And each item opens another scene/window. But you can open as many as you want. They contain different data, but same fxml/scene.
Is this possible?
Yes, it is possible. It would require you to create a controller for the scene, and then create a new instantiation of that controller object for each new instance of that scene. I think it will also require passing it a new stage each time.
EDIT: Failing to create a new controller instance for each new instance of a scene would mean it is possible to overwrite data in an open scene in one window from an open scene in another window.
In case you're newer to JavaFX, I'll provide some examples of creating new views. This question is mostly related to methods of going about doing so.
Assuming you're following MVC pattern, your project may be structured similar to this:
EDIT: Corrected improper usage of FXMLLoader
MainApp.java
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Modality;
import javafx.stage.Stage;
import Controller.LoginController;
public class Main
extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage stage) throws Exception
{
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/View/Login.fxml"));
Parent root = loader.load();
stage.setScene(new Scene(root));
LoginController loginController = loader.getController();
// Pass this stage by calling the controller's stage setter.
// Conversely we could call a singleton class here to
// give us a stage object that we can re-use many times
loginController.setStage(stage);
stage.initStyle(StageStyle.UNIFIED);
stage.setResizable(false);
stage.getIcons().add(new Image("Resources/icon.png"));
stage.setTitle("");
stage.centerOnScreen();
stage.show();
} catch (IOException ex) {
// exception handling
}
}
}
LoginController.java
import java.net.URL;
import java.io.IOException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.stage.Modality;
import javafx.stage.Stage;
import Controller.MainController;
public class LoginController implements Initializable
{
#FXML
private Button loginButton;
private Stage stage;
public LoginController() {}
#Override
public void initialize(URL url, ResourceBundle rb) {
loginButton.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
doLogin();
});
}
#FXML
private void doLogin() {
// handle login
showMainApplicationView();
}
#FXML
private void showMainApplicationView() {
try {
loader = new FXMLLoader(LoginController.class.getResource("View/mainView.fxml"));
Parent root = (Parent) loader.load();
MainController mainController = new MainController();
loader.setController(mainController);
mainController.setStage(new Stage());
stage.initModality(m);
stage.setResizable(isResizable);
stage.getIcons().add(new Image("Resources/icon.png"));
stage.setTitle(title);
stage.setScene(new Scene(root));
return stage;
} catch (IOException e) {
// exception handling
}
}
#FXML
public void setStage(Stage stage) {
this.stage = stage;
}
}
MainController.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
public class LoginController implements Initializable
{
public LoginController() {}
#Override
public void initialize(URL url, ResourceBundle rb) {}
#FXML
private void showSubview() {
/* Same code as the prior examples for launching a new view
* It will work for creating as many new "windows" of the same
* view as you desire. If you need to pass different types of
* data to each view you can use a DAO model, or some kind of
* "Instance" object that works for sending/receiving data to/from
* the subview from this class.
*/
}
#FXML
public void setStage(Stage stage) {
this.stage = stage;
}
}
If you want to reuse a stage more than once and at varying depths into subviews I would recommend using a Singleton class.
StageHolder.java
import javafx.stage.Stage;
public final StageHolder
{
private Stage stage;
private final static StageHolder INSTANCE = new StageHolder();
private StageHolder() {}
public static StageHolder getInstance() {
return INSTANCE;
}
public Stage getStage() {
return this.stage;
}
// you can omit this setter if you only want to re-use a single stage over and over.
public void setStage(Stage stage) {
this.stage = stage;
}
}

Update label from nested function called from Task API in JavaFX

I am performing some background task using this class
class Download extends Task{
protected Object call() throws Exception {
try {
updateMessage("Establishing Connection");
DownloadHelper downloadHelper = new DownloadHelper();
downloadHelper.performTask();
return null;
} catch (IOException | ParseException ex) {
logger.error(ExceptionUtils.getStackTrace(ex));
throw ex;
}
}
}
This Task in turn calls DownloadHelper to perform some task.
class DownloadHelper{
public DownloadHelper(){
}
public void performTask(){
----
----
}
}
Is there a way to update the status message of the Task API (updateMessage()) from the DownloadHelper class.?
The expedient approach is to pass a reference to the Download task as a parameter to the DownloadHelper constructor. To minimize coupling, you can instead pass a reference to your implementation of updateMessage() as a parameter of type Consumer, "an operation that accepts a single input argument and returns no result."
DownloadHelper helper = new DownloadHelper(this::updateMessage);
Your helper's implementation of performTask() can then ask the updater to accept() messages as needed.
Consumer<String> updater;
public DownloadHelper(Consumer<String> updater) {
this.updater = updater;
}
public void performTask() {
updater.accept("Helper message");
}
A related example is seen here.
import java.util.function.Consumer;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* #see https://stackoverflow.com/q/45708923/230513
*/
public class MessageTest extends Application {
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("MessageTest");
StackPane root = new StackPane();
Label label = new Label();
root.getChildren().add(label);
Scene scene = new Scene(root, 320, 120);
primaryStage.setScene(scene);
primaryStage.show();
Download task = new Download();
task.messageProperty().addListener((Observable o) -> {
label.setText(task.getMessage());
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
private static class Download extends Task<String> {
#Override
protected String call() throws Exception {
updateMessage("Establishing connection");
DownloadHelper helper = new DownloadHelper(this::updateMessage);
helper.performTask();
return "MessageTest";
}
#Override
protected void updateMessage(String message) {
super.updateMessage(message);
}
}
private static class DownloadHelper {
Consumer<String> updater;
public DownloadHelper(Consumer<String> updater) {
this.updater = updater;
}
public void performTask() {
updater.accept("Helper message");
}
}
public static void main(String[] args) {
launch(args);
}
}

JavaFx stop scene [duplicate]

I'm building a JavaFX application with multiple Scenes. I have a problem with scope of variable when changing scenes within setOnAction event. This is my code:
Stage myStage;
public Scene logInScene(){
... all the buttons / textFields
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
**this.getStage().allScene(createAccountPane1);**
}
}
}
public Stage getStage(){
return this.myStage;
}
public void allScene(Pane p){
this.myStage.setScene(p);
}
I'm getting an error within the setOnAction function. "Cannot Find Symbol" getStage(). I know this must be a scope problem and it doesn't recognize any variables / functions outside of that scope. How do I make it so that I can change within? I've tried passing through the variable but that will just make my code messy and I wish there was a simpler way. Thanks guys!
Your code works as long as you keep consistency:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application{
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
Scene scene = logInScene();
primaryStage.setScene(scene);
primaryStage.show();
}
public Scene logInScene(){
Pane root = new Pane();
Button createAccountButton = new Button("create account");
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
stage.setScene(CreateAccountScene());
}
});
root.getChildren().add(createAccountButton);
return new Scene(root);
}
protected Scene CreateAccountScene() {
VBox root = new VBox();
Label userLabel = new Label("Insert the username:");
final TextField userField = new TextField();
Button createAccountButton = new Button("create account");
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
System.out.println("Account for user " + userField.getText() + " was created succesfully");
}
});
root.getChildren().addAll(userLabel,userField,createAccountButton);
return new Scene(root);
}
public static void main(String[] args) {
launch(args);
}
}
This question has already been solved, but I think it's worth clarifying that your line fails because the this keyword refers to the anonymous EventHandler you are implementing. In Java, you reference the outer class instance with OuterClass.this. So OuterClass.this.getStage().allScene(createAccountPane1); will work.
If you are looking for a prettier solution, some coders like to define a local variable that points to the outer class instance:
final OuterClass self = this;
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
self.getStage().allScene(createAccountPane1);
}
}

JavaFX - Control and Concurrency

I have a sample Hello World JavaFx. I am using Eclipse and eFxclipse plugin. My Eclipse is kepler which is Eclipse 4.3.2 version and Java servion is Jdk1.7-045.
What I try to add is very little concurrency codes, I just want to update button text in the example. Could this backend task interact with upfront UI control, for example button, scene? If not, how could I make tailored backend task, then interact with UI control?
Thanks in advance
package com.juhani.fx.exer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application{
private static final short COREPOOLSIZE=2;
private static final short MAXIMUMPOOLSIZE=2;
private static final int WORKQUEUECAPACITY=100;
private static Logger log = LogManager.getLogger(
HelloWorld.class.getName());
private ExecutorService executors = new ThreadPoolExecutor(COREPOOLSIZE,MAXIMUMPOOLSIZE,20,TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));
public static void main(String[] args) {
LogMessage logMessage = new LogMessage("BEGIN",1.0,1,"HELLOWORLD");
log.trace(logMessage.toString());
launch(args);
}
#Override
public void start(final Stage primaryStage) throws InterruptedException {
primaryStage.setTitle("Hello World!");
final Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
final StackPane root = new StackPane();
root.getChildren().add(btn);
final Scene scene= new Scene(root,300,250);
primaryStage.setScene(scene);
primaryStage.show();
Task<Boolean> task = new Task<Boolean>() {
#Override
public Boolean call() {
for(int i=0;i<20;i++){
btn.setText("First row\nSecond row "+i);
primaryStage.show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error(new LogMessage("entering interruption",1.0,2,"exception").toString());
}
}
return new Boolean(true);
}
};
executors.submit(task);
}
}
This answer specially talks about the use of Platform.runLater. If you are using Task, you are better off updating the UI using the method it provides as stated in
kleopatra's answer.
For updating the UI, you have to be on the Javafx thread.
Once you are on any other thread, use Platform.runLater() to update those data back to Javafx UI. A working example can be found below
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
private static final short COREPOOLSIZE = 2;
private static final short MAXIMUMPOOLSIZE = 2;
private static final int WORKQUEUECAPACITY = 100;
private ExecutorService executors = new
ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE, 20, TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) throws InterruptedException {
primaryStage.setTitle("Hello World!");
final Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
final StackPane root = new StackPane();
root.getChildren().add(btn);
final Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
Task<Boolean> task = new Task<Boolean>() {
#Override
public Boolean call() {
final AtomicInteger i = new AtomicInteger(0);
for( ; i.get() < 20; i.incrementAndGet()) {
Platform.runLater(new Runnable() {
#Override
public void run() {
btn.setText("First row\nSecond row " + i);
}
});
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
return Boolean.valueOf(true);
}
};
executors.submit(task);
}
}
For more information you can go through the links provided here
A Task is designed to interact with the ui on the fx-application thread, to take advantage of that support you should use it as designed :-)
As a general rule, you must not access ui in the call method [*] of the Task. Instead, update one of its properties (message, progress ...) and bind that property to your ui. Sample code:
Task<Boolean> taskWithBinding = new Task<Boolean>() {
#Override
public Boolean call() {
final AtomicInteger i = new AtomicInteger(0);
for( ; i.get() < 20; i.incrementAndGet()) {
updateMessage("First row\nSecond row " + i);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
};
btn.textProperty().bind(taskWithBinding.messageProperty());
[*] The one exception is outlined (wrap the access into an runLater) in the other answer. Doing so is technically correct - but then you are by-passing a Task's abilities and could use an arbitrary Runnable ...

Using a JavaFx application instance from another class

I have a MainWindowFx class like below. It basically creates a simple JavaFX GUI.
package drawappfx;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.control.TextAreaBuilder;
/**
*
* #author Hieu
*/
public class MainWindowFX extends Application{
public static final int DEFAULT_WIDTH = 600;
public static final int DEFAULT_HEIGHT = 600;
private int width;
private int height;
private Scene scene;
private TextArea messageView;
private Button quitButton;
private BorderPane layout;
private Stage primaryStage;
#Override
public void start(Stage primaryStage) {
System.out.println("Started building GUI....");
this.buildGUI();
System.out.println("Finished building GUI");
this.primaryStage = primaryStage;
primaryStage.setTitle("Hello World!");
primaryStage.setScene(this.scene);
primaryStage.show();
System.out.println("Where the hell are you?");
}
public Scene getScene() {
return this.scene;
}
public BorderPane getBorderPane() {
return this.layout;
}
public Stage getPrimaryStage() {
return this.primaryStage;
}
public void buildGUI() {
System.out.println("Before layout");
this.layout = new BorderPane();
System.out.println("Before vbox");
this.layout.setBottom(this.addVBox());
System.out.println("before new scene");
this.scene = new Scene(this.layout, DEFAULT_WIDTH, DEFAULT_HEIGHT);
System.out.println("after new scene");
}
public VBox addVBox() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(15, 12, 15, 12));
// message box
this.messageView = TextAreaBuilder.create()
.prefRowCount(5)
.editable(false)
.build();
// quit button
this.quitButton = new Button("Quit");
this.quitButton.setPrefSize(100, 20);
System.out.println("think of a good message?");
vbox.getChildren().addAll(this.messageView, this.quitButton);
System.out.println("before returning vbox");
return vbox;
}
public void postMessage(final String s) {
this.messageView.appendText(s);
}
}
Now I want to use an instance of this object in another class:
package drawappfx;
import java.io.InputStreamReader;
import java.io.Reader;
import javafx.scene.layout.BorderPane;
public class DrawAppFx
{
public static void main(String[] args)
{
final MainWindowFX main = new MainWindowFX();
BorderPane layout = main.getBorderPane();
Reader reader = new InputStreamReader(System.in);
Parser parser = new Parser(reader,layout,main);
main.start(main.getPrimaryStage());
parser.parse();
}
}
But when I run this I ran into this error:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.javafx.main.Main.launchApp(Main.java:658)
at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:397)
at javafx.scene.Scene.<init>(Scene.java:287)
at javafx.scene.Scene.<init>(Scene.java:226)
at drawappfx.MainWindowFX.buildGUI(MainWindowFX.java:74)
at drawappfx.MainWindowFX.start(MainWindowFX.java:47)
at drawappfx.DrawAppFx.main(DrawAppFx.java:39)
... 6 more
Java Result: 1
I've done some searches on this and guessed that it has something to do with threading... but I still have no idea. Any suggestions?
I've had this problem several times and there is a fairly easy way to resolve it.
First of all let me introduce you to the Mediator pattern, basically you want to create a class that has the relationship with all your GUI classes
(I.e the different GUI classes do not have their own instance of each other instead all of them has the same reference to the Mediator).
That was a sidetrack now to your question.
In order to change window you need to pass the Stage of which the new window should be placed upon because of this your code needs only a minor change:
Now I do not often do this but in your case, I will make an exception the following code consists of a class that you can "Copy Paste" into your program and use that will fix the problem after the code I will explain exactly what I did:
Mediator
public class Mediator extends Application {
private DrawAppFx daf;
private MainWindowFX mainWindow;
private Stage primaryStage;
public Mediator(){
daf = new DrawAppFx(this);
mainWindow = new MainWindowFx(this);
}
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
mainWindow.start(primaryStage);
}
public void changeToDaf(){
daf.start(primaryStage);
}
}
Now each of the DrawAppFx and MainWindowFx must have a constructor that passes a Mediator object and therefore have a "Has-a" relationship with the mediator
The reason behind this is that the mediator pattern is now in control and should you create more windows it is easy to implement just add them to the mediator and add a method for which the mediator can change to that window.

Resources