Hallo,
ich bin noch relativ frisch mit JavaFX unterwegs und wollte mal fragen, wie Ihr eine JavaFX-Applikation mit mehreren Scenes in der Praxis lösen würdet.
Folgende Ausgangslage:
Ich möchte genre eine kleine "Testanwendung" schreiben in der zunächst ein Anmeldedialog (Scene) erscheinen soll. Nach erfolgter Anmeldung wird der Dialog geschlossen und die eigentliche Applikation (andere Scene) wird gestartet. Hört sich einfach an, aber ich stehe echt auf dem Schlauch...
Hier mal der Code:
LoginDialog.fxml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="153.0" prefWidth="217.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.userlogin.FXMLController">
<children>
<GridPane layoutX="7.0" layoutY="15.0">
<children>
<TextField fx:id="textfieldUsername" prefWidth="200.0" promptText="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<PasswordField id="passwordfirldPassword" fx:id="passwordfieldPassword" prefWidth="200.0" promptText="Password" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label id="textfieldUsername" text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Label alignment="TOP_LEFT" contentDisplay="TOP" prefWidth="200.0" text="User Login" GridPane.columnIndex="0" GridPane.columnSpan="2147483647" GridPane.rowIndex="0">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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>
</GridPane>
<HBox alignment="CENTER_RIGHT" layoutY="105.0" prefHeight="41.0" prefWidth="207.0">
<children>
<Button fx:id="buttonLogin" alignment="CENTER_RIGHT" contentDisplay="CENTER" mnemonicParsing="false" onAction="#buttonLogin" prefHeight="21.999908447265625" text="Login" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
</children>
</AnchorPane>
[/XML]
Ich müsste in der Klasse FXMLController nach der Aufruf des "buttonLogin" die aktuelle Scene schliessen und eine neue Scene öffnen. Nur wie mache ich das? Komme ich von der FXMLController Klasse irgendwie an die Stage aus der App Klasse? Der FXMLController wird ja durch den FXMLLoader geladen, so dass ich keine Referenz übergeben kann.
Hat jemand eine Idee? Oder bin ich gar völlig auf dem Holzweg?
Viele Grüße
Markus
ich bin noch relativ frisch mit JavaFX unterwegs und wollte mal fragen, wie Ihr eine JavaFX-Applikation mit mehreren Scenes in der Praxis lösen würdet.
Folgende Ausgangslage:
Ich möchte genre eine kleine "Testanwendung" schreiben in der zunächst ein Anmeldedialog (Scene) erscheinen soll. Nach erfolgter Anmeldung wird der Dialog geschlossen und die eigentliche Applikation (andere Scene) wird gestartet. Hört sich einfach an, aber ich stehe echt auf dem Schlauch...
Hier mal der Code:
Java:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("LoginDialog.fxml"));
stage.setTitle("User Login");
stage.setScene(new Scene(root));
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class FXMLController implements Initializable{
@FXML private TextField textfieldUsername;
@FXML private PasswordField passwordfieldPassword;
@FXML protected void buttonLogin(ActionEvent event){
System.out.println("Login | Username: "+ textfieldUsername.getText()+" | Password: "+ passwordfieldPassword.getText()); // + passwordfieldPassword.getText()
// Scene schliessen und Application starten.... HIER HÄNGE ICH FEST!!!
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
LoginDialog.fxml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="153.0" prefWidth="217.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.userlogin.FXMLController">
<children>
<GridPane layoutX="7.0" layoutY="15.0">
<children>
<TextField fx:id="textfieldUsername" prefWidth="200.0" promptText="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<PasswordField id="passwordfirldPassword" fx:id="passwordfieldPassword" prefWidth="200.0" promptText="Password" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label id="textfieldUsername" text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Label alignment="TOP_LEFT" contentDisplay="TOP" prefWidth="200.0" text="User Login" GridPane.columnIndex="0" GridPane.columnSpan="2147483647" GridPane.rowIndex="0">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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>
</GridPane>
<HBox alignment="CENTER_RIGHT" layoutY="105.0" prefHeight="41.0" prefWidth="207.0">
<children>
<Button fx:id="buttonLogin" alignment="CENTER_RIGHT" contentDisplay="CENTER" mnemonicParsing="false" onAction="#buttonLogin" prefHeight="21.999908447265625" text="Login" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
</children>
</AnchorPane>
[/XML]
Ich müsste in der Klasse FXMLController nach der Aufruf des "buttonLogin" die aktuelle Scene schliessen und eine neue Scene öffnen. Nur wie mache ich das? Komme ich von der FXMLController Klasse irgendwie an die Stage aus der App Klasse? Der FXMLController wird ja durch den FXMLLoader geladen, so dass ich keine Referenz übergeben kann.
Hat jemand eine Idee? Oder bin ich gar völlig auf dem Holzweg?
Viele Grüße
Markus