Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap screens in a JavaFX application in the controller class?

If there are 3 files in a JavaFX project; an FXML file, a controller for the FXML, and an application class; how can the controller respond to a button click (which works perfectly fine) with changing the screen on that click (which is normally done with stage.setScreen())? I have no reference to the stage (which is passed to the application class's start(Stage)).

Application sample:

public class JavaFXApplication4 extends Application {      @Override     public void start(Stage stage) throws Exception {         Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));          Scene scene = new Scene(root);          stage.setScene(scene);         stage.show();     }      /**      * The main() method is ignored in correctly deployed JavaFX application.      * main() serves only as fallback in case the application can not be      * launched through deployment artifacts, e.g., in IDEs with limited FX      * support. NetBeans ignores main().      *      * @param args the command line arguments      */     public static void main(String[] args) {         launch(args);     } } 

FXML sample:

<?xml version="1.0" encoding="UTF-8"?>  <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?>  <AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0"  xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication4.SampleController">   <children>   <Button id="button" fx:id="nextScreen" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Next Screen" />   <Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />   </children> </AnchorPane> 

Controller sample:

public class SampleController implements Initializable {      @FXML     private Label label;      @FXML     private void handleButtonAction(ActionEvent event) {         System.out.println("You clicked me!");         label.setText("Hello World!");          // Here I want to swap the screen!     }      @Override     public void initialize(URL url, ResourceBundle rb) {         // ...     }     } 
like image 493
B Y E Avatar asked Oct 09 '12 16:10

B Y E


People also ask

How do I specify a controller in JavaFX?

There are two ways to set a controller for an FXML file. The first way to set a controller is to specify it inside the FXML file. The second way is to set an instance of the controller class on the FXMLLoader instance used to load the FXML document.

How do I move something in JavaFX?

Just click on the circle and drag it. The vehicles will follow it.

How do controllers work in JavaFX using FXML?

Specifying Inside the FXML File Directly Following is a syntax to Specifying inside the FXML file directly. Explanation: fx:controller is used to including controller class. Button onAction attribute used for performing clicking action.


1 Answers

@FXML private void handleButtonAction(ActionEvent event) {     System.out.println("You clicked me!");     label.setText("Hello World!");     //Here I want to swap the screen!      Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow();     // OR     Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow();     // these two of them return the same stage     // Swap screen     stage.setScene(new Scene(new Pane())); } 
like image 171
Uluk Biy Avatar answered Oct 11 '22 21:10

Uluk Biy