Question

Please have a look at the code snippets:

import java.io. *;
* import java.sql.;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
javafx.animation import *.;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class Book extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("book");
        stage.setScene (scene);
        . stage.getIcons () add (new Image ("icon.png"));
        / / Stage.setFullScreen (true) / / Works
        stage.show ();



    }

    @ FXML
    public void fullscreen (ActionEvent event)
    {

        / / Stage.setFullScreen (true) / / Does not work
        / / Can not find symbol (stage)

    }

If I do NOT use FXML, then it works (:-D):

import java.io. *;
* import java.sql.;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
javafx.animation import *.;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.animation.TranslateTransitionBuilder;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class Book extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("book");
        stage.setScene (scene);
        . stage.getIcons () add (new Image ("icon.png"));
        stage.show ();

        btn.setOnAction (new EventHandler <ActionEvent> ()
        {
            public void handle (ActionEvent evt)
            {
                stage.setFullScreen (true);
            }

        });

    }

The reason for this solution is that the event handler of the button is now in the start method.

The @ FXML annotation to use within the start method does not work (of course):

public class Book extends Application implements Initializable
{

@ Override
    public void start (Stage primaryStage) throws IOException
    {

        Stage stage = primaryStage;

        Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);

        Scene scene = new Scene (root);

        stage.setTitle ("book");
        stage.setScene (scene);
        . stage.getIcons () add (new Image ("icon.png"));
        stage.show ();

* /
       @ FXML
    public void fullscreen (ActionEvent event)
    {

        stage.setFullScreen (true) / / Does not work
        / / Can not find symbol (stage)

    }
/ * / / Will not work


    }

My question is: How can I use the stage variable anywhere? Or is there another solution ? JavaFX is cool, just a shame that there is not a good IDE (with GUI designer). The JavaFX Scene Builder you can forget and to work with FXML is silly! :-( However, an alternative to the JavaFX Scene Builder is not known to me. And the GUI without creating GUI designer is silly for large programs :-(.

Please help me.

On the internet there is NO answer to my question, whhat surprised me. (Neither German nor English tutorials)

And sorry for my bad English. I learn the language :-). And I am a Java beginner, too :-).

I hope I did not forget anything:-D.

Thanks :-)

Was it helpful?

Solution

Declare a static Stage variable in the class and assign your primaryStage to it.

public class Book extends Application implements Initializable
{
    public static Stage stage;
    @ Override
    public void start (Stage primaryStage) throws IOException
    {
        stage = primaryStage;
        ...
    }
    ...
    @ FXML
    public void fullscreen (ActionEvent event)
    {
        stage.setFullScreen(true);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top