JavaFX Stage not visible - java

I'm trying to write the simplest code to start learning how JavaFX works. Here is my code:
package app;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class mainClass extends Application{
Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
StackPane layout = new StackPane();
Scene scene = new Scene(layout, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
It should just show an empty window. My compilator gives no errors, but nothing shows up, even if I can see my program running in my dock. How is this possible? I'm sure I'm missing some stupid simple thing, but I cannot figure out what it is.

Related

Window not showing up on Mac JAVA

package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class Main extends Application {
Button button;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("GAME");
button = new Button();
button.setText("CLICK HERE");
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
stage.setScene(scene);
stage.show();
}
}
Trying to run but the window won't show up, any thoughts on how to fix. I've gone through other posts but no luck. By the way, I'm using IntelliJ. Just started learning JavaFX.
This is in my .bash_profile:
export PATH_TO_FX=/usr/local/bin/javafx-sdk-14/lib
alias javafxc="javac --module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml,javafx.media,javafx.web,javafx.swing"
alias javafx="java --module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml,javafx.media,javafx.web,javafx.swing"
To compile:
javafxc Main.java
To run:
javafx Main

I cannot seem to change scenes in javafx

So I'm trying to simply change between two scenes in javafx, but I've come into this re-occurring problem that I can't seem to fix. It is demonstrated in the following code:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class TestApplication extends Application
{
private Stage stage;
private Scene scene, scene2;
public void start(Stage s)
{
scene=new Scene(new Group());
scene2=new Scene(new Group());
scene.setFill(Color.GREEN);
scene2.setFill(Color.ORANGE);
scene.setOnMouseClicked(e-> changeScene(scene2));
scene2.setOnMouseClicked(e-> changeScene(scene));
stage=s;
s.setScene(scene);
s.show();
}
public void changeScene(Scene nex)
{
stage.setScene(nex);
System.out.println("here");
}
public static void main(String[] args)
{
launch(args);
}
}
Am I doing something wrong? How can I fix this?
What's going wrong
You are not placing anything in the scenes (just an empty group). By default scenes are (usually) going to size to the preferred size of their contained content. As your scenes have no content of any size, then the scenes shouldn't really have any size. I think the fact that the first scene even shows up is a bit of a quirk of the JavaFX system where it seems to set some default size to the initial scene when it can't work out any preferred size for the scene (just so that the initial window shows up).
How to fix it
To fix it, put some content in the enclosed scenes (and/or set the initial scene size in your scene constructors).
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class TestApplication extends Application {
private Stage stage;
private Scene scene, scene2;
public void start(Stage s) {
scene = new Scene(new Group(new Label("1")), 200, 150);
scene2 = new Scene(new Group(new Label("2")), 200, 150);
scene.setFill(Color.GREEN);
scene2.setFill(Color.ORANGE);
scene.setOnMouseClicked(e -> changeScene(scene2));
scene2.setOnMouseClicked(e -> changeScene(scene));
stage = s;
s.setScene(scene);
s.show();
}
private void changeScene(Scene nex) {
stage.setScene(nex);
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX multiple line

When i tried to compile code it have bug. I am trying to resolve problem one hour and i don't know this bug. Do you know how to fix it? Thanks!
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public abstract class Mainn extends Application
{
public static void main(String[] args){launch(args);
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass.getResource("/fxml/StackPaneWindow.fxml"));
StackPane stackPane = loader.load();
Scene scene = new Scene (stackPane);
primaryStage.setScene(scene);
primaryStage.setTitle("bang");
primaryStage.show();
}
}
}
The first problem I see with this is that the method start(Stage) is inside your main function, which isn't allowed java. Close your main function before defining another function.

Why doesn't button add to scene?

package application;
import java.awt.Button;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
public class Main extends Application {
Button button; //Declare Button
public static void main(String[] args) {
launch(args); //calls application
}
#Override
public void start(Stage primaryStage) throws Exception {
//comes from application pack
primaryStage.setTitle("Title Of Window"); //Main Stage
button = new Button (); //Creates Button
button.setLabel("Click Me");
StackPane layout = new StackPane();
layout.getChildren().add(button); //Button in Scene
Scene scene = new Scene (layout, 300, 250); //Sets Scene
primaryStage.setScene(scene); //Stage and Scene
primaryStage.show();
}
Hey guys, so this is first time I am creating something in JavaFX and I was wondering why doesn't my button in Scene add in Layout.GetChildren() line, it keeps on displaying red line underneath add. I am using Eclipse IDE.
you have imported import java.awt.Button, you must import Button class in the javafx package.

Sending notification with javafx without stage

You can send a desktop notification with JavaFx like this (requires jdk 8u20 or later):
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button notifyButton = new Button("Notify");
notifyButton.setOnAction(e -> {
Notifications.create().title("Test").text("Test Notification!").showInformation();
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(notifyButton, 100, 50));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But this way you have to create a main window (stage), is it possible to avoid this? I am looking for a way to send notification like using zenity in bash: most of the code is non-gui, but uses some gui elements for informing or interacting with user in a very basic fashion.
It looks like the ControlsFX notifications require a existing stage. You can create a hidden utility stage. Try something like this.
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
}
public static void main(String[] args) {
new JFXPanel();
notifier("Good!", "It's working now!");
}
private static void notifier(String pTitle, String pMessage) {
Platform.runLater(() -> {
Stage owner = new Stage(StageStyle.TRANSPARENT);
StackPane root = new StackPane();
root.setStyle("-fx-background-color: TRANSPARENT");
Scene scene = new Scene(root, 1, 1);
scene.setFill(Color.TRANSPARENT);
owner.setScene(scene);
owner.setWidth(1);
owner.setHeight(1);
owner.toBack();
owner.show();
Notifications.create().title(pTitle).text(pMessage).showInformation();
}
);
}
}
new JFXPanel() initializes the JavaFX thread without having to extend Application. You have to call this before any calls to Platform.runLater() otherwise you will get a thread exception. You only need to call it once for the whole application though. Honestly it is probably better to create your own notification stage and display it directly. Create a stage like above and put your own contents. You can probably reuse some of the styling from the ControlsFX source.

Categories

Resources