Eclipse doesn't want to run simple JavaFX application - java

I have other classes named Test in other packages and one class with the same name in the default package.
When I click the Run button in Eclipse, instead of running this class, it runs another Test class from inside another package instead:
package jfx;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Test extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
}
How can I fix this?

Add a main method to allow Eclipse recognize the program as runnable application
public static void main(String[] args) {
Application.launch(args);
}

Related

why call method launch(args) equivalent to call method Application.launch(args)

I am learning Javafx and wondering why this calling launch(args) in this code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
public class Gui extends Application{
#Override
public void start(Stage primaryStage){
Button btn = new Button("OK");
Scene scene = new Scene(btn, 200, 250);
primaryStage.setTitle("My First GUI");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(true);
}
public static void main(String[] args) {
Application.launch(args);
}
}
is equivalent when we call
launch(args);
I've searched and found this answer "the JavaFX main class is a subtype of Application." but I can't understand it.
It's because you extended your gui class with javafx Application class. In other words, you inherited all of its methods including static void launch.

Full-screen Overlay

Currently, I am trying to create a timer that displays in the corner of your screen. I want it to show even if there is currently a full-screened application running. Currently I've tried Stage#setAlwaysOnTop(true), however that only functions for normal applications, not when they are full-screened.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Timer");
stage.setScene(new Scene(root, 300, 275));
stage.setAlwaysOnTop(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the standard JavaFX example with IntelliJ, however with the one modification which was Stage#setAlwaysOnTop(true). How would I get this to function ontop of the full-screen, or at least stay omnipresent no matter the application.

Show a variable's value in JavaFX

I'm currently working on a capstone project for a Java class and a problem I'm coming across frequently is displaying a variable's value in a JavaFX scene. I need a kickstart to get me moving, my google searches aren't bearing any fruit.
Thanks all :)
You can use a Label. Attach it to your scene and call Label.setText(String text) with the string representation of your variable value. Here's a complete example, using a Label:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class main3 extends Application {
static Integer variable = 250; // The value will be displayed in the window
#Override public void start(Stage primaryStage) {
Label variableLabel = new Label();
variableLabel.setFont(new Font(30));
variableLabel.setText("" + variable);
variableLabel.setLayoutX(175);
variableLabel.setLayoutY(125);
Group group = new Group(variableLabel);
Scene scene = new Scene(group, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
Result

Is it possible to embed an external program in JavaFX?

I'm currently developing a JavaFX desktop application that will take the place of an existing browser based application.
This new application have the option of launching external applications made in swing.
I'm now wondering if there's any chance of launching these external applications inside the existing JavaFX application?
In other words, is there a way to embed external applications in JavaFX?
Do you mean something like this?
JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button fxButton = new Button("javafx button");
fxButton.setOnAction(event -> new SwingTest());
Scene scene = new Scene(fxButton, 600, 400);
stage.setScene(scene);
stage.show();
}
}
Swing:
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Button;
public class SwingTest extends JFrame {
SwingTest() {
Button swingButton = new Button("swing button");
getContentPane().add(swingButton);
setSize(600, 600);
setVisible(true);
}
}

MiGLayout sizing not working properly in JavaFX

Here's my snippet:
package javafxdemo;
import org.tbee.javafx.scene.layout.MigPane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class FXDemo extends Application {
#Override
public void start (Stage stage) throws Exception {
MigPane root = new MigPane();
Scene scene = new Scene(root);
Button b = new Button("Hello");
root.getChildren().add(b);
stage.setScene(scene);
stage.setTitle("FX");
stage.show();
}
public static void main (String[] args) {
launch (args);
}
}
When running the gui doesn't show properly: the frame size is smaller than the button. Why does it happens? In HBox Layout when setting the scene it is automatically resized, so why with MiGLayout it doesn't work?
I'm using MigLayout 4.3
So, I filed an issue and later found out a workaround for this:
just add stage.sizeToScene() after stage.show().

Categories