-
-
Save jewelsea/4631319 to your computer and use it in GitHub Desktop.
<?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.*?> | |
<StackPane prefWidth="231.0" xmlns:fx="http://javafx.com/fxml" fx:controller="login.LoginController"> | |
<children> | |
<StackPane> | |
<children> | |
<VBox spacing="10.0"> | |
<children> | |
<GridPane> | |
<children> | |
<Label text="Username:" GridPane.columnIndex="0" GridPane.rowIndex="0" /> | |
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="1" /> | |
<TextField fx:id="user" promptText="Use "open" to login" text="open" GridPane.columnIndex="1" GridPane.rowIndex="0" /> | |
<TextField fx:id="password" promptText="Use "sesame" to login" text="sesame" GridPane.columnIndex="1" GridPane.rowIndex="1" /> | |
</children> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="148.0" minWidth="10.0" prefWidth="109.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="228.0" minWidth="10.0" prefWidth="189.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
</GridPane> | |
<StackPane prefHeight="-1.0" prefWidth="-1.0"> | |
<children> | |
<Button fx:id="loginButton" alignment="CENTER" defaultButton="true" mnemonicParsing="false" text="Login" StackPane.alignment="CENTER_RIGHT" /> | |
</children> | |
</StackPane> | |
</children> | |
</VBox> | |
</children> | |
</StackPane> | |
</children> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</StackPane> |
package login; | |
import javafx.event.*; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.*; | |
/** Controls the login screen */ | |
public class LoginController { | |
@FXML private TextField user; | |
@FXML private TextField password; | |
@FXML private Button loginButton; | |
public void initialize() {} | |
public void initManager(final LoginManager loginManager) { | |
loginButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent event) { | |
String sessionID = authorize(); | |
if (sessionID != null) { | |
loginManager.authenticated(sessionID); | |
} | |
} | |
}); | |
} | |
/** | |
* Check authorization credentials. | |
* | |
* If accepted, return a sessionID for the authorized session | |
* otherwise, return null. | |
*/ | |
private String authorize() { | |
return | |
"open".equals(user.getText()) && "sesame".equals(password.getText()) | |
? generateSessionID() | |
: null; | |
} | |
private static int sessionID = 0; | |
private String generateSessionID() { | |
sessionID++; | |
return "xyzzy - session " + sessionID; | |
} | |
} |
package login; | |
import java.io.IOException; | |
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.*; | |
import javafx.stage.Stage; | |
import login.LoginManager; | |
/** Main application class for the login demo application */ | |
public class LoginDemoApplication extends Application { | |
public static void main(String[] args) { launch(args); } | |
@Override public void start(Stage stage) throws IOException { | |
Scene scene = new Scene(new StackPane()); | |
LoginManager loginManager = new LoginManager(scene); | |
loginManager.showLoginScreen(); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} |
package login; | |
import java.io.IOException; | |
import java.util.logging.*; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.*; | |
/** Manages control flow for logins */ | |
public class LoginManager { | |
private Scene scene; | |
public LoginManager(Scene scene) { | |
this.scene = scene; | |
} | |
/** | |
* Callback method invoked to notify that a user has been authenticated. | |
* Will show the main application screen. | |
*/ | |
public void authenticated(String sessionID) { | |
showMainView(sessionID); | |
} | |
/** | |
* Callback method invoked to notify that a user has logged out of the main application. | |
* Will show the login application screen. | |
*/ | |
public void logout() { | |
showLoginScreen(); | |
} | |
public void showLoginScreen() { | |
try { | |
FXMLLoader loader = new FXMLLoader( | |
getClass().getResource("login.fxml") | |
); | |
scene.setRoot((Parent) loader.load()); | |
LoginController controller = | |
loader.<LoginController>getController(); | |
controller.initManager(this); | |
} catch (IOException ex) { | |
Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private void showMainView(String sessionID) { | |
try { | |
FXMLLoader loader = new FXMLLoader( | |
getClass().getResource("mainview.fxml") | |
); | |
scene.setRoot((Parent) loader.load()); | |
MainViewController controller = | |
loader.<MainViewController>getController(); | |
controller.initSessionID(this, sessionID); | |
} catch (IOException ex) { | |
Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
<?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.*?> | |
<StackPane prefWidth="231.0" xmlns:fx="http://javafx.com/fxml" fx:controller="login.MainViewController"> | |
<children> | |
<StackPane> | |
<children> | |
<VBox spacing="10.0"> | |
<children> | |
<GridPane> | |
<children> | |
<Label text="Session ID:" GridPane.columnIndex="0" GridPane.rowIndex="0" /> | |
<Label fx:id="sessionLabel" text="Undefined" GridPane.columnIndex="1" GridPane.rowIndex="0" /> | |
</children> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="148.0" minWidth="10.0" prefWidth="109.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" maxWidth="228.0" minWidth="10.0" prefWidth="189.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
</GridPane> | |
<StackPane prefHeight="-1.0" prefWidth="-1.0"> | |
<children> | |
<Button id="loginButton" fx:id="logoutButton" alignment="CENTER" defaultButton="true" mnemonicParsing="false" text="Logout" StackPane.alignment="CENTER_RIGHT" /> | |
</children> | |
</StackPane> | |
</children> | |
</VBox> | |
</children> | |
</StackPane> | |
</children> | |
<padding> | |
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | |
</padding> | |
</StackPane> |
package login; | |
import javafx.event.*; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.*; | |
/** Controls the main application screen */ | |
public class MainViewController { | |
@FXML private Button logoutButton; | |
@FXML private Label sessionLabel; | |
public void initialize() {} | |
public void initSessionID(final LoginManager loginManager, String sessionID) { | |
sessionLabel.setText(sessionID); | |
logoutButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent event) { | |
loginManager.logout(); | |
} | |
}); | |
} | |
} |
this one has alot of warnings when run
Thanks for this .You should make a change at
stage.setScene(scene);
stage.show();
to
primarystage.setScene(scene);
primarystage.show();
Thanks for this .You should make a change at
stage.setScene(scene);
stage.show();
to
primarystage.setScene(scene);
primarystage.show();
depend on what ur variable
I retried running this in 2023 with JavaFX 20 to see if there were any issues with it and it ran perfectly fine for me with no additional changes and no warnings displayed.
As an alternate solution, you might consider adopting the approach documented in: https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx
Thanks for this .You should make a change at
stage.setScene(scene);
stage.show();
to
primarystage.setScene(scene);
primarystage.show();depend on what ur variable
You are correct. Thanks for the correction.
Answer to the StackOverflow question: How can I use a variable from another Controller in JavaFX, which requests a login demo application with transfer of login session information to the main application controller.