Launch JavaFX application from another class - javafx-8

I need to start a javafx Application from another "container" class and call functions on the Application, but there doesn't seem to be any way of getting hold of a reference to the Application started using the Application.launch() method. Is this possible?
Thanks

Suppose this is our JavaFX class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class OKButton extends Application {
#Override
public void start(Stage stage) {
Button btn = new Button("OK");
Scene scene = new Scene(btn, 200, 250);
stage.setTitle("OK");
stage.setScene(scene);
stage.show();
}
}
Then we may launch it from another class like this:
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(OKButton.class, args);
}
}

I had the same problem as this and got round it using this hack:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.concurrent.CountDownLatch;
public class StartUpTest extends Application {
public static final CountDownLatch latch = new CountDownLatch(1);
public static StartUpTest startUpTest = null;
public static StartUpTest waitForStartUpTest() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return startUpTest;
}
public static void setStartUpTest(StartUpTest startUpTest0) {
startUpTest = startUpTest0;
latch.countDown();
}
public StartUpTest() {
setStartUpTest(this);
}
public void printSomething() {
System.out.println("You called a method on the application");
}
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
Label label = new Label("Hello");
pane.setCenter(label);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
and then the class you are launching the application from:
public class StartUpStartUpTest {
public static void main(String[] args) {
new Thread() {
#Override
public void run() {
javafx.application.Application.launch(StartUpTest.class);
}
}.start();
StartUpTest startUpTest = StartUpTest.waitForStartUpTest();
startUpTest.printSomething();
}
}
Hope that helps you.

Launch JavaFX in other Class using Button:
class Main extends Application{
public void start(Stage s)throws Exception{
event();
s.show();
}
public void event(){
btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae){
Stage s = new Stage();
new SubClassName().start(s);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
class SubClassName{
public void start(Stage s){
Pane pane = new Pane();
Scene addFrame = new Scene(pane,280,450);
s.setScene(addFrame);
s.show();
}
}

I'm not sure what you're trying to achieve, but note that you can e.g call from another class Application.launch to start the JavaFX Application thread and Platform.exit to stop it.

The above ways of invoking other javafx class from another sometimes work. Struggling to find an ultimate way to do this brought me to the following walk around:
Suppose this is the javafx class that exteds Application we wish to show from a different class, then we should add the following lines
class ClassToCall extends Application{
//Create a class field of type Shape preferably static...
static Stage classStage = new Stage();
#Override
public void start(Stage primaryStage){
// Assign the class's stage object to
// the method's local Stage object:
classStage = primaryStage ;
// Here comes some more code that creates a nice GUI.....
// ......
}
}
And now from the other place in the project, in order to open the window
that the above class creates do the following:
// Suppose we want to do it with a button clicked:
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//create an object of the class you wish to invoke its
//start() method:
ClassToCall ctc = new ClassToCall();
// Then call its start() method in the following way:
ctc.start(ClassToCall.classStage);
}// End handle(ActionEvent event)
});// End anonymous class

Related

How to run JavaFX Window in other Class

as in title, i need help with Running JavaFX in other class. For exmaple, from Main i want to type something like 'new TempClass()' and then i expect new Window but i got nothing xD Thank you in advance for solve the problem! <3
Main:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
new TempClass();
}
public static void main(String[] args) {
launch(args); // when i launch(args) the 'start' func starting btw
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is one of few ways which solved my problem:
Main:
public class Main {
public static void main(String[] args) {
Application.launch(TempClass.class, args);
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Error in scene in Java FX

I'm new to javaFX 8 and working with a simple project, but im having some issues with scene.
I get this error:
Error:(35, 25) java: method getScene in class javafx.stage.Window cannot be applied to given types;
required: no arguments
found: javafx.scene.Scene
reason: actual and formal argument lists differ in length
And this is the code:
package ch.makery.address;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application{
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.getScene(scene);
primaryStage.show();
}catch (IOException e) {
e.printStackTrace();
}
}
public void showPersonOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view.Personview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
rootLayout.setCenter(personOverview);
}catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
Thanks in advance.

Read JavaFx Application

I created a small javaFx Program. The Program just displays random points. Now I want to get and read the generated Stage from a different class and read the co-ordinates. Is it possible ?
I tried creating a class variable to get stage but its null always.
I added a class variable and assigned the prepared Stage object to the variable. I am then trying to get the Stage object from the variable.
public class DCGUI extends Application {
private Stage primaryStage1;
public static void main(String[] args) {
launch(args);
Platform.exit();
}
#Override
public void start(Stage primaryStage) throws Exception {
Random r = new Random(64);
// List<Integer> points = r.ints(1000, 0, 400).boxed().collect(Collectors.toList());
List<Node> cList = new ArrayList<>();
Line line = null;
Circle c = null;
for (int i = 0; i < 50001; i++) {
//System.out.println(r.nextInt());
c = new Circle(r.nextInt(400), r.nextInt(400), 0.0125);
c.setStroke(Color.RED);
c.setId(i+"");
cList.add(c);
}
Group group = new Group(cList);
Scene scene = new Scene(group, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Dynamic Connectivity");
primaryStage1 = primaryStage;
primaryStage.show();
}
Let's assume the following code example. By calling the getMainStage() method you can have access to your Stage object.
Of course, the main.fxml file is binded with the MainController class
Main.java
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader("path/to/main.fxml").toFile().toURI().toURL());
MainController.setMainStage(stage); <---- !
Parent root = loader.load();
stage.setScene(new Scene(root, 1400, 850));
stage.show();
}
public static void main(String[] args) {
launch(args);
}}
MainController.java
public class MainController{
// fxml view elements...
private static Stage mainStage;
//...
public static Stage getMainStage() {
return mainStage;
}
}

How to create an instance of A class that extends Application

I just designed a simple javaFx app. While running it solo works, but when I try to separated and create an instance of it all I get :
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
my code
import javafx.application.Application;
import javafx.stage.Stage;
public class Demo
{
public static void main(String[] args)
{
Demos dm = new Demos();
}
}
class Demos extends Application {
private String args;
private Stage stage;
public Demos()
{
main(args);
start(stage);
}
public void main(String args)
{
this.args=args;
launch(this.args);
}
#Override
public void start(Stage stage)
{
this.stage=stage;
this.stage.setTitle("Simple JavaFX Application");
this.stage.setResizable(false);
this.stage.show();
}
}
Application.launch requires the Application class to be lauched to be public. This is not the case for your Demos class.
Additional Notes
private String args;
private Stage stage;
public Demos()
{
main(args);
...
}
public void main(String args)
{
this.args=args;
...
}
Just assigns the initial value of args to itself, which will always result in args remaining null.
Application.launch is a static method creating the Application instance itself. Calling this form from a instance makes little sense.
If you want to launch a specific Application, pass the Application class to Application.launch:
public static void main(String[] args) {
Application.launch(Demos.class);
}
public Demos extends Application {
private Stage stage;
public Demos(){
}
#Override
public void start(Stage stage) {
this.stage=stage;
this.stage.setTitle("Simple JavaFX Application");
this.stage.setResizable(false);
this.stage.show();
}
}

GWT.setUncaughtExceptionHandler()

Has anyone successfully used the above statement to catch the exception before it goes to the browser as an alert?.
I registered a custom Exception Handler in the first line of my application entry point. But it does not catch the exception as expected.
public void onModuleLoad(){
GWT.setUncaughtExceptionHandler(new MyExceptionHandler());
...
....
}
EDIT
Here are my two classes:
I expect my system.out will print the details of the exception
and exception will be swallowed and should not be sent to browser.
Or Am I wrong?
package mypackage;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
public class MyEntryPoint implements EntryPoint {
public void onModuleLoad() {
GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
startApplication();
}
private void startApplication() {
Integer.parseInt("I_AM_NOT_NUMBER");
}
}
package mypackage;
import com.google.gwt.core.client.GWT;
public class ClientExceptionHandler implements GWT.UncaughtExceptionHandler {
#Override
public void onUncaughtException(Throwable cause) {
System.out.println(cause.getMessage());
}
}
I believe what's happening here is that the current JS event cycle is using the DefaultUncaughtExceptionHandler because that was the handler set at the start of the cycle. You'll need to defer further initialization to the next event cycle, like this:
public void onModuleLoad() {
GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
startApplication();
Window.alert("You won't see this");
}
});
}
private void startApplication() {
Integer.parseInt("I_AM_NOT_A_NUMBER");
// or any exception that results from server call
}
Update: And here's the issue that describes why this works, and why it isn't planned to be fixed.
Setting up a default handler can be a tricky proposition some times. I can tell you exactly what is going on. If you get an exception in the onModuleLoad(), the handler will not be called. It is only after the load method is completed that it will ACTUALLY get put into place.
You should try the following:
public void onModuleLoad(){
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
onUncaughtException(Throwable t) {
// Do stuff here
}
});
}
and see if that helps.
Silly solution, but it works fine!
before anything add your EntryPoint in your app.gwt.xml
<entry-point class='myPackage.client.MyEntryPoint' />
then;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
public class MyEntryPoint implements EntryPoint {
private EventBus eventBus;
#Inject
public MyEntryPoint(final EventBus eventBus){
this.eventBus = eventBus;
}
#Override
public void onModuleLoad() {
CustomUncaughtExceptionHandler customUncaughtExceptionHandler = new CustomUncaughtExceptionHandler();
GWT.setUncaughtExceptionHandler(customUncaughtExceptionHandler);
try {
onModuleLoad2();
} catch (RuntimeException ex) {
eventBus.fireEvent(new BusyEvent(false));
customUncaughtExceptionHandler.onUncaughtException(ex);
}
}
private void onModuleLoad2() {
throw new RuntimeException("test");
}
}
and your CustomUncaughtExceptionHandler would be something like:
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.user.client.Window;
public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
#Override
public void onUncaughtException(Throwable e) {
Throwable exceptionToDisplay = getExceptionToDisplay( e );
Window.alert( exceptionToDisplay.getCause() .getMessage()+" "+ exceptionToDisplay.getStackTrace());
}
private static Throwable getExceptionToDisplay( Throwable throwable ) {
Throwable result = throwable;
if (throwable instanceof UmbrellaException && ((UmbrellaException) throwable).getCauses().size() >= 1) {
result = ((UmbrellaException) throwable).getCauses().iterator().next();
}
return result;
}
}