I'm having some difficulty with loading a method once my popup window has loaded. I've gotten the code to work, but only once you click a button. I'm new to JavaFX, this is the first thing I've written with it.
I've posted the signUp Controller and PopUp FXML. The issue is when you click Enter after entering your username, a popup box should appear with the username and your random 4 digit code. I can't get my code to change userShown and passShown to the username you entered and your random digits, though it will change if I assign a button to do this action by calling setLabels().
The code is a bit messy as I've been mucking around trying to figure out ways to get this to work, so there may be bits that aren't needed.
There may also be a lot of imports due to my many failed attempts at trying to get this to work. I know this isn't the way you should store a username/code, but it's more of a mini system to stop family members using not your account. It's not being distributed just a sort of test to learn JavaFX.
signUp Controller:
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.ResourceBundle;
import org.omg.CORBA.INITIALIZE;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class signUpController {
@FXML
private TextField userName;
@FXML
private Label userShown;
@FXML
private Label passShown;
Writer write = new Writer();
static public String uName;
static public int uCode;
int number = (int)( Math.random() * 9999);
static boolean done = false;
public void homeButton(ActionEvent event) throws IOException{
Parent signUpParent = FXMLLoader.load(getClass().getResource("Splash.fxml"));
Scene signUpScene = new Scene(signUpParent);
//This line gets the Stage information
// Make it a node type object -> get source -> get the scene -> get the window -> window = window(stage)
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(signUpScene);
window.centerOnScreen();
window.show();
window.setResizable(false);
}
// Passing Login Details to Writer to Write to Users.txt
public void signUpButton(ActionEvent event) throws IOException{
// To make sure number is higher than 999
if (number < 1000) {
number += 1000;
}
uCode = number;
uName = userName.getText();
// Write Username/Code to Txt Doc
System.out.println(userName.getText() + number);
write.writeStart(userName.getText(),number);
loginPopUp();
setLabels();
if (done == true) {
homeButton(event);
done = false;
}
// Reset Number
number = (int)( Math.random() * 9999);
}
// Giving Login Details
public void loginPopUp()throws IOException {
Stage window2 = new Stage(StageStyle.UTILITY);
Parent loginPopUpParent = FXMLLoader.load(getClass().getResource("loginPopUp.fxml"));
Scene loginPopUpScene = new Scene(loginPopUpParent);
window2.initModality(Modality.APPLICATION_MODAL);
window2.setScene(loginPopUpScene);
window2.setResizable(false);
window2.centerOnScreen();
window2.show();
done = true;
}
private void setLabels() {
userShown.setText(uName);
passShown.setText(Integer.toString(uCode));
}
public void close(ActionEvent event){
Stage window2 = (Stage)((Node)event.getSource()).getScene().getWindow();
window2.close();
}
}
Sign Up FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="signUpController">
<top>
<ImageView fitHeight="96.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="@Hangman.png" />
</image>
<BorderPane.margin>
<Insets left="20.0" top="50.0" />
</BorderPane.margin>
</ImageView>
</top>
<center>
<VBox alignment="CENTER" spacing="15.0">
<children>
<Label text="Please Enter Your Chosen Username">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
<TextField fx:id="userName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="192.0" promptText="Username" />
<Button mnemonicParsing="false" onAction="#signUpButton" text="Enter" />
<Button layoutX="288.0" layoutY="171.0" mnemonicParsing="false" onAction="#homeButton" text="Back" />
</children>
<BorderPane.margin>
<Insets top="20.0" />
</BorderPane.margin>
</VBox>
</center>
<bottom>
<Pane BorderPane.alignment="CENTER">
<children>
<Pane layoutX="279.0" layoutY="8.0" />
</children>
</Pane>
</bottom>
</BorderPane>
Login Pop Up FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<VBox alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="267.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="signUpController">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Username:" wrappingWidth="122.45364984856826">
<font>
<Font size="26.0" />
</font>
</Text>
<Label fx:id="userShown" text="Name Here">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Password Code:" wrappingWidth="183.45364984856826">
<font>
<Font size="26.0" />
</font>
</Text>
<Label fx:id="passShown" text="Code Here">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#close" prefHeight="25.0" prefWidth="53.0" text="Close">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
<padding>
<Insets bottom="50.0" left="15.0" top="50.0" />
</padding>
</VBox>
Any help would be vastly appreciated, if there's any guides or tutorials that are great for teaching JavaFX could you please point me in the right direction?