Why is main method used in JavaFX Application when start() already exist - java

The starting point for a JavaFX application is start method. But in the sample JavaFX applications, there is a main method included as well. What is the use of main method in this particular case and why was there a need to define start() as starting point for JavaFX. Couldn't we simply use the main method to define a starting point like Swings?
A sample HelloWorld application:
public class HelloWorld extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Hello World");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

From Oracle Docs ,
The main() method is not required for JavaFX applications when the JAR
file for the application is created with the JavaFX Packager tool,
which embeds the JavaFX Launcher in the JAR file. However, it is
useful to include the main() method so you can run JAR files that were
created without the JavaFX Launcher, such as when using an IDE in
which the JavaFX tools are not fully integrated. Also, Swing
applications that embed JavaFX code require the main() method.

Related

How do i change the javafx intellij template to my own template if i create a new javafx application?

I do not want to use the Intellij IDEA template anymore when creating javafx programs. I want to use my own template because I can work a lot better with it. Here is an example of what I mean.
Intellij template, I do not want to use this anymore:
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
My template, I want to use this template:
#Override
public void start(Stage primaryStage) throws Exception{
StackPane root = new StackPane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
You can create a user defined template with quite simple steps.
create a new project using the build-in JavaFX template
amend the Main class with your changes
in the Tools menu select Save project as template...
- save it e.g. as JavaFX own
- after that it will be available in the new project wizard in category User-defined

"Hello World" app in Java FX with minimal Java code

I selected Java FX between it and Swing because I want that view (*ML) and logic will be split (impossible in Swing).
Here is the "Hello world" template in IntelliJ IDEA:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Two meddlings to View in the Logic part:
Setting title - setTitle()
Setting top-level element size: primaryStage.setScene(new Scene(root, 300, 275));.
Can I set same settings in FXML?
The FXML loader will give you the root element, which already has a prefWidth and prefHeight.
You can't set the title but you can drop the size for the scene and it will be taken from the root element automatically.
Another thing that has to be set for the Stage explicitly are size constraints. You can have an initial size but the user can drag it around however he/she wants.
You can set size using the
prefHeight="yourHeightValue" prefWidth="yourWidthValue"
sets in your scene's root element. You can create scenes using SceneBuilder and it will generate related FXML for you.

Button cannot be covert to node, method list add(node) is not applicable

I'm beginner at JavaFx. I tried to run basic things i.e button but i'm not able due to some compilation error i.e. Button cannot be covert to node, method list add(node) is not applicable This is my simple code to run program in netbeans
Code
public class Hello extends Application{
Button btn;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("I'm beginner");
btn=new Button("Click me");
StackPane root=new StackPane();
root.getChildren().add(btn);
Scene scene=new Scene(root,350,400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Most probably you used the button from
java.awt package. Make sure that the Button is from javafx.scene.control.Button package.
I had similar problem once and wasted an hour on that stupid mistake.
Check the imports. It should be javafx.scene.control.Button for Button and javafx.scene.layout.StackPane for StackPane.
Is it correct?
Check out http://code.makery.ch/library/javafx-8-tutorial/.
And http://gluonhq.com/products/scene-builder/
The method described in the tutorial combined with the new scene builder makes creating a javafx gui insanely easy.

How can I open a JavaFX GUI from JavaSwing GUI?

I created a swing GUI, and a graph using JavaFX. I would like to open the graph by clicking a button on the swing GUI. My Code is below. Thank you for your help.
This is what I have to open the FX GUI from the swing GUI.
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Test Graph = new Test();
Graph.GrpahScreen();
}
});
FX class
public class Test extends Application {
#Override
public void start(Stage stage) {
// TODO
}
public static void main(String args []) {
launch(args);
}
}
The Application class represents a JavaFX Application. Since you are writing a Swing application (with JavaFX content embedded in it), it makes no sense to create an Application subclass.
Additionally, it is highly recommended not to use both JavaFX Stages and Swing JFrames in the same application. You should place the JavaFX content in a JFXPanel and display the JFXPanel in a JFrame. You need to be careful to obey the threading restrictions of both toolkits: as usual, Swing components must be created on the AWT event dispatch thread, and JavaFX components must be created on the JavaFX application Thread. This is all covered in detail in the JFXPanel documentation, but in essence:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
Platform.runLater(() -> {
Parent root = ... ; // create JavaFX content, can be in a separate class
Scene scene = new Scene(root);
jfxPanel.setScene(scene);
});
frame.add(jfxPanel);
frame.setSize(...);
frame.setVisible(true);
}
});

Add a JPanel in a BorderPane

I have a Java project that plays a .flv media file through JavaFX Media Player, and it's working fine. Recently, I've been wanting to experiment by adding GUI components to this Project (JPanel, JLabel). However, I've failed in all my attempts and after doing some research turns out it's not as simple as i first thought.. I've tried borderPane.setTop(JLabel) but I get a "Cannot convert Jlabel to Node" error.. I feel that I'm missing something
If anyone has any idea why this isnt working for me, I would greatly appreciate any form of explanation or examples.. :)
Here is the code if it might be of use to you!
#Override
public void start(Stage stage){
String path = "Data/Video/Clip.flv";
Media media = new Media(new File(path).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(mediaView);
//borderPane.add(logoPanel); <<<<<<< Error
Scene scene = new Scene(borderPane, 1024, 800);
scene.setFill(javafx.scene.paint.Color.BLACK);
stage.setTitle("Media Player");
stage.setScene(scene);
stage.show();
mediaPlayer.setAutoPlay(true);
BorderPane is a JavaFX Node whereas JPanel is a Java Swing Component.
You cannot add a JPanel to a BorderPane, what you are looking for instead is the JavaFX equivalent of the JPanel which is the Pane class.
If you are developing using JavaFX it is easier to just use JavaFX components. If you must use Swing components then you can use the SwingNode class.
What you basically want to achieve: add some Swing components to an JavaFX application.
You have to use SwingNode class to "wrap" a JComponent.
SwingNode Class
JavaFX 8 introduces the SwingNode class, which is located in the
javafx.embed.swing package. This class enables you to embed Swing
content in a JavaFX application. To specify the content of the
SwingNode object, call the setContent method, which accepts an
instance of the javax.swing.JComponent class.
You can check this tutorial how embed Swing component into JavaFX application.
Small example to put a Swing Button into the center of a BorderPane:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
SwingNode swingNode = new SwingNode();
JButton jButton = new JButton("I am a Swing button");
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Message from Swing");
}
});
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
swingNode.setContent(jButton);
}
});
root.setCenter(swingNode);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
By the way, BorderPane has no method "add" (as in your commented line):
You can use setCenter, setTop, setBottom, setLeft and setRight to add Nodes into it (as you used to fill the center).
If your goal was not to embed Swing into JavaFX, just to use JavaFX controls, you can check this article what controls JavaFX has by default.

Categories

Resources