Last active
September 28, 2019 14:43
-
-
Save nemo404/e62f9bff87a17beb0185eeccfda9513f to your computer and use it in GitHub Desktop.
Opening new Window (Stage) and closing it, in JavaFx using the FXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.text.Font?> | |
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.pland.dc.Fenetre1Controller"> | |
<children> | |
<Button fx:id="Button1" layoutX="240.0" layoutY="269.0" mnemonicParsing="false" onAction="#new_window" prefHeight="34.0" prefWidth="102.0" text="Ok" /> | |
<Label fx:id="label1" alignment="CENTER" layoutX="197.0" layoutY="88.0" prefHeight="36.0" prefWidth="205.0" text="FENETRE 1" textFill="#2172ff"> | |
<font> | |
<Font name="System Bold" size="25.0" /> | |
</font> | |
</Label> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import javafx.fxml.FXML; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.stage.Modality; | |
import javafx.stage.Stage; | |
public class Fenetre1Controller { | |
@FXML | |
private Button Button1;// déclaration des variables obligatoires sinon elles ne s'affichent pas | |
@FXML | |
private Label label1; | |
@FXML | |
private void new_window() { | |
try { | |
FXMLLoader loader = new FXMLLoader(); | |
loader.setLocation(Main.class.getResource("Fenetre2.fxml")); | |
AnchorPane page = (AnchorPane) loader.load(); | |
Stage stage = new Stage(); | |
Scene scene = new Scene(page); | |
stage.initModality(Modality.APPLICATION_MODAL);// on ne peut pas écrire dans les autres fenêtres | |
Fenetre2Controller controller = loader.getController();//Returns the controller associated with the root object. | |
controller.setStage(stage); | |
stage.setTitle("Fenetre2"); | |
stage.setScene(scene); | |
stage.show(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.ColumnConstraints?> | |
<?import javafx.scene.layout.GridPane?> | |
<?import javafx.scene.layout.RowConstraints?> | |
<AnchorPane prefHeight="358.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.pland.dc.Fenetre2Controller"> | |
<children> | |
<GridPane layoutX="52.0" layoutY="85.0" prefHeight="168.0" prefWidth="257.0"> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="137.0" minWidth="10.0" prefWidth="82.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="175.0" minWidth="10.0" prefWidth="175.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Label fx:id="labelnom" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Nom" /> | |
<Label fx:id="labelprenom" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Prenom" GridPane.rowIndex="1" /> | |
<Label fx:id="labelmail" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Mail" GridPane.rowIndex="2" /> | |
<TextField fx:id="textfieldnom" GridPane.columnIndex="1" /> | |
<TextField fx:id="textfieldprenom" GridPane.columnIndex="1" GridPane.rowIndex="1" /> | |
<TextField fx:id="textfieldmail" GridPane.columnIndex="1" GridPane.rowIndex="2" /> | |
</children> | |
</GridPane> | |
<Button fx:id="butonvalider" layoutX="175.0" layoutY="290.0" mnemonicParsing="false" onAction="#Valider" prefHeight="33.0" prefWidth="82.0" text="Valider" /> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.fxml.FXML; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.stage.Stage; | |
public class Fenetre2Controller { | |
@FXML | |
private Label labelnom; | |
@FXML | |
private Label labelprenom; | |
@FXML | |
private Label labelmail; | |
@FXML | |
private Button butonvalider; | |
@FXML | |
private TextField textfieldmail; | |
@FXML | |
private TextField textfieldnom; | |
@FXML | |
private TextField textfieldprenom; | |
private Stage stage; | |
public void setStage(Stage stage) { | |
this.stage = stage; | |
} | |
@FXML | |
private void Valider() { | |
stage.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.stage.Stage; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Parent root = FXMLLoader.load(getClass().getResource("Fenetre1.fxml")); | |
Scene scene = new Scene(root,600,400); | |
primaryStage.setTitle("Test Fenetres"); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment