Reputation: 99
I have already asked the question but it was not answered.
How can I switch to full screen mode in JavaFX by pressing a button? But the button is created with FXML (JavaFX Scene Builder). (cannot find symbol (stage) ) If the button is created manually, then it works.
public class Buch 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 ("Buch");
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
/ / cannot find symbol (stage)
}
Works:
public class Buch 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 ("Buch");
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);
}
});
}
Does not work (of course ?):
public class Buch 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 ("Buch");
stage.setScene (scene);
stage.getIcons () add (new Image ("Icon.png"));
stage.show ();
* /
@ FXML
public void fullscreen (ActionEvent event)
{
stage.setFullScreen (true) / / Does not work
/ / cannot find symbol (stage)
}
/ * / / Will not work
}
You can also take a look at this link. The question is more detailed here:
stackoverflow.com/questions/22820049/full-screen-under-javafx-with-fxml-does-not-work
How can I use the stage variable anywhere? Or is there another solution ? Please help me.
On the internet there is NO answer to my question!?!
I am a Java beginner. :-)
Thanks for your help
Upvotes: 3
Views: 6100
Reputation: 209694
Why is your Application also your controller? That seems like it won't work, full screen functionality or no full screen functionality.
In the controller, just inject the button (or any node, but the button would be the obvious one), and call getScene()
and getWindow()
in the event handler:
public class MyController {
@FXML
private Button fullScreenButton ;
@FXML
private void fullScreen(ActionEvent event) {
Stage stage = (Stage) fullScreenButton.getScene().getWindow();
stage.setFullScreen(true);
}
}
Upvotes: 7
Reputation: 21
I'm not a JavaFX Expert, but this won't work in pure Java too. The DI Container injects the method fullscreen(event) which doesn't know stage, declared in start().
Did u try to move it up to a class member ? public class Buch { private Stage stage; ... }
Upvotes: 1