I want to create a program in Java with a main window, that can open more subprograms in other windows.
I created a simple JavaFX program for the main window, and it works as expected, like so:
public class MainThread extends Application {
#Override
public void start(Stage primaryStage) {
// code goes here...
}
}
... and I created other Java classes the same way.
I tried to run them simply with new SubProgramThread();- as I would create a new instance of any object- but that hasn't worked for me- it doesn't run the start() method in the subprogram classes.
Is that the right way to create an Application instance?
Thanks.
I do this all the time. I have a menu of items. When a user clicks the item button I create the new window by calling start() on the FX class:
new TyAltSvr().start(new Stage());
Some people don't like calling start() but it hasn't caused me any problems.
Related
I'm trying to add a functionality to my app which changes the language.
I wrote a following controller for this -> https://pastebin.com/n3nXdAx8
I'm having a problem with starting my JavaFx Application, when I run it I get a following error https://hastebin.com/agakacubuv.js
I tried to fix it following this topic Starting JavaFX from Main method of class which doesn't extend Application
It's my Main class https://pastebin.com/6s8iqcUe
try changing
public static Stage stage=new Stage();
to
public static Stage stage;
You initialize the stage in start() method anyway.
Edit
The second thing is that there is no such key as bundles.main.view.application.title (Main:32). try to use main.view.application.title instead.
everyone. I've been searching for this question but I haven't found it here, so I'll guess it's really simple.
I'm creating a very simple application in JavaFX with a single button. Now I want to handle its events (like when it's pressed or when it's released), but when I see examples over the Internet, they all use anonymous classes (and a different class for each event), which makes the code dirty in my opinion. That's why I want to put the event handlers in a separate class and add them to the button.
The problem is that I don't know if I have to create a different class for every event, which I think isn't cool. So I came up with an idea. In the handle() method of the class I check which type of event is going on and process it.
This is the code
Main class
public class Main extends Application{
Button button;
PruebaEventHandler evhandler;
public Main() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("h0i");
button = new Button("PĂșlsame!");
evhandler = new PruebaEventHandler();
button.addEventHandler(MouseEvent.ANY, evhandler);
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
EventHandler class
public class PruebaEventHandler implements EventHandler<MouseEvent>{
#Override
public void handle(MouseEvent event){
if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED)){
System.out.printf("Te cogĂ\n");
}
if(event.getEventType().equals(MouseEvent.MOUSE_RELEASED)){
System.out.printf("\nMe ha soltado!!!");
}
}
}
I don't know if this is very inefficient or bad programming style, but is the only solution I've come up with so far. So I want to ask you if this is a good solution or, if there's a better one, shed your light on me! Thanks beforehand.
There already exists a solution for this, which is creating a JavaFX-project with a FXML-file, Controller and also a Main class. IDEs like IntelliJ and NetBeans have support for letting you create a JavaFX-project, which automatically creates those files for you, but I'm not sure if you have to add a plugin to make it work, that I don't remember.
The FXML-file takes care of the GUI, for example placing a button in a scene, and the easiest way to use it is with a SceneBuilder, which Oracle has, and can also be integrated in your IDE.
If you use FXML you can direct buttons to methods inside your FXML-document, so you don't need to have anonymous classes for event handlers. Instead you make the button call a spesific method in your Controller-class.
Here are a couple of youtube-tutorials that showcase the basics of using JavaFX with FXML:
https://www.youtube.com/watch?v=K7BOH-Ll8_g
https://www.youtube.com/watch?v=LMdjhuYSrqg
The problem I'm having is that when I create a new JavaFX project in NetBeans the main method is ignored, and somehow start() is somehow called and everything is just fine, but any time I try to call start I wind up with an exception. The class I used:
public final class JFXDriver extends Application {
public JFXDriver() {
Application.launch();
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
I've attempted to start it using the constructor, but it fails (Throws an Exception) for some reason saying that it is being called more than once, which should be impossible because I've constructed this class from a main method with only the new JFXDriver(); in it.
You are doing it wrong in the code. You should not call launch in your constructor. You should call it in your main and pass the name of the class that extends Application.
This causes the system to call init and then start and thus begins the lifecycle of your applicaton. For a more detailed explanation have a look here: http://codelatte.wordpress.com/2013/11/15/getting-started-with-javafx-hello-world-2/
Are you attempting a Swing and FX interop ?
Alright, I've found a solution to the problem. I added:
public static void start() {
Application.launch();
}
and took out the call to Application.launch() in the constructor. This approach worked. I guess that the JavaFX thread created its own instance of the class leading to the Application.launch() being called more than once. Interestingly, without the one application limit, I wonder if this would have led to a StackOverflowException due to the recursive nature of the call.
I am using Swing Application Framework JSR(296) for my Swing based Java application.
Similar to AboutBox, I have followed the usage of #Action and added some JDialog classes to my project.
The problem is, when I close the main frame, my application still runs in background.
To overcome this I added following code to the configureWindow() of my main application class:
protected void configureWindow(java.awt.Window root) {
root.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// write your code here
Application.getInstance(MyApp.class).exit();
}
});
}
But with this modification whenever I close the the dialog (including AboutBox), it also closes the main frame.
What should I do to prevent the entire application from exiting and just close the dialog box?
Update:
I am using NetBeans IDE 7.01 which allows to create Swing Application Framework project.
It generates a project skeleton as shown below:
MyApp
|--myapp
| |--MyApp.java
| |--MyAppAboutBox.java
| |--MyAppView.java
|
|--myapp.resources
|--MyApp.properties
|--MyAppAboutBox.properties
|--MyAppView.properties
NetBeans IDE allows to add actions from Window->Properties menu.
MyApp class extends org.jdesktop.application.SingleFrameApplication which is my main class.
MyAppView extends FrameView which is my main view.
Implementation classes of javax.swing.JDialog are in the myapp.view package.
The WindowEvent class has a method call getWindow(), which returns the window that is closing.
Inside your windowClosing method you can check: if the window is the main application window, use the code that you currently have. If it is not, just call Window.dispose()
Edit: I didn't notice that you were creating custom dialogs in your application. Maybe you forget to dispose them? You should add code like the one in the auto generated about box:
#Action public void closeAboutBox() {
dispose();
}
and call this action whenever the dialog closes. If this is not the problem, a thread dump will probably help you in order to find out which thread is running when you close the main window.
I think what you are actually looking for is setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. Have a look at the JavaDoc API...
In my code, I have used :
Application.launch(MyDesign.getClass(), null);
to run a UI of MyDesign from a seperate class.
When it calls launch(), it executes the constructor of MyDesign class. I have created a MyDesign object earlier and initialized its attributes. So I got a new object after calling launch(). I need to stop the calling its constructor when I call
Application.launch().
Your feed back is highly appreciated. Thanks.
Unfortunately you can't directly run already created Application through Application.launch().
You can use next stub application and launch it instead:
public class Runner extends Application {
#Override
public void start(Stage stage) throws Exception {
myDesignInstance.start(stage);
}
}