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();
}
}
Related
I would like to collect metrics with Vert.x Micrometer Metrics, so I need to set proper options to VertxOptions. I run Vertx with Launcher and there is a hook beforeDeployingVerticle but when I override it it's not called.
I overriden Launcher class and beforeDeployingVerticle method but this method is never executed.
public class LauncherTest {
public static class SimpleVerticle extends AbstractVerticle {
#Override
public void start(Future<Void> startFuture) throws Exception {
System.out.println("verticle started");
}
}
public static class LauncherWithHook extends Launcher {
#Override
public void beforeDeployingVerticle(DeploymentOptions deploymentOptions) {
System.out.println("before deploying");
}
}
public static void main(String[] args) {
new LauncherWithHook().execute("run", SimpleVerticle.class.getName());
}
}
In a result I receive just verticle started, but I expect also to have before deploying there. Should I add this hook somehow different?
change your main method like this:
public static void main(String[] args) {
String[] argz = {"run", "your.namepace.LauncherTest$SimpleVerticle"};
LauncherWithHook launcher = new LauncherWithHook();
launcher.dispatch(argz);
}
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;
}
}
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();
}
}
I thing it is a very silly question but I don't manage to to this. If you made a FXML-Template Project with Java, you got automatically three files. The view in XML, the controller and the start file in java.
I want to use the scene in the controller class but I don't know how to make a reference to do this.
Here is my example:
public class CatchTheScene extends Application {
private Scene scene;
private Parent root;
#Override
public void start(Stage stage) throws Exception {
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
FXMLDocumentController controller = new FXMLDocumentController(this);
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
/**
* #return the scene
*/
public Scene getScene() {
return scene;
}
/**
* #param scene the scene to set
*/
public void setScene(Scene scene) {
this.scene = scene;
}
}
public class FXMLDocumentController implements Initializable {
private CatchTheScene c;
#FXML
private Label label;
#FXML
private Button button;
#FXML
private AnchorPane root;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
public FXMLDocumentController(CatchTheScene c)
{
this.c = c;
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
c.getScene().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
System.out.println("I am the scene and have been clicked");
}
});
}
}
I solved it now. First I gave in the Scene Builder a Code FX:ID named root to the root pane.
root is in the controller an object and I registered on root an EventListener.`
#FXML
private Canvas canvas;
#FXML
private AnchorPane root;
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent e) {
if (e.getCode() == KeyCode.LEFT) {
clownfish.setDx(-10);
clownfish.moveLeft();
}
if (e.getCode() == KeyCode.RIGHT) {
clownfish.setDx(-10);
clownfish.moveRight();
}
if (e.getCode() == KeyCode.UP) {
clownfish.setDy(-10);
clownfish.moveUp();
}
if (e.getCode() == KeyCode.DOWN) {
clownfish.setDy(-10);
clownfish.moveDown();
}
}
});
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