title: JavaFX - Insering login window into my main function
body: <p>I am very much new to JavaFX, I started this week basing my knowledge solely off of small tutorials and sample projects learning the syntax. I have created a simple Table of Stock information that you can add and delete information in and would like to implement a login window to this program. I created the login window but am unsure of how to implement it in my main function properly. </p>
  
  <p>Main.java </p>
  
  <p>This contains my code for the stock application.</p>
  
  <pre><code>package sample;
  import javafx.application.Application;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXMLLoader;
  import javafx.geometry.Insets;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.control.TextField;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  
  public class Main extends Application {
  
      Stage window;
      TableView&lt;Stock&gt; table;
      TextField symbolInput, nameInput, openingPriceInput, closingPriceInput, changeInPriceInput;
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          window = primaryStage;
          window.setTitle("Stock Application");
  
          //Symbol column
          TableColumn&lt;Stock, String&gt; symbolColumn = new TableColumn&lt;&gt;("Symbol");
          symbolColumn.setMinWidth(100);
          symbolColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("symbol"));
  
          //Name column
          TableColumn&lt;Stock, String&gt; nameColumn = new TableColumn&lt;&gt;("Name");
          nameColumn.setMinWidth(100);
          nameColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("name"));
  
          //Opening Price column
          TableColumn&lt;Stock, Double&gt; openingPriceColumn = new TableColumn&lt;&gt;("Opening Price");
          openingPriceColumn.setMinWidth(100);
          openingPriceColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("openingPrice"));
  
          //Closing Price column
          TableColumn&lt;Stock, Double&gt; closingPriceColumn = new TableColumn&lt;&gt;("Closing Price");
          closingPriceColumn.setMinWidth(100);
          closingPriceColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("closingPrice"));
  
          //Change in Price column
          TableColumn&lt;Stock, Double&gt; changeInPriceColumn = new TableColumn&lt;&gt;("Change in Price");
          changeInPriceColumn.setMinWidth(100);
          changeInPriceColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("changeInPrice"));
  
          //Symbol input
          symbolInput = new TextField();
          symbolInput.setPromptText("Symbol");
          symbolInput.setMinWidth(100);
  
          //Name input
          nameInput = new TextField();
          nameInput.setPromptText("Name");
  
          //Opening Price input
          openingPriceInput = new TextField();
          openingPriceInput.setPromptText("Opening Price");
  
          //Closing Price input
          closingPriceInput = new TextField();
          closingPriceInput.setPromptText("Closing Price");
  
          //Change in Price Input
          changeInPriceInput = new TextField();
          closingPriceInput.setPromptText("Change in Price");
  
          //Button
          Button addButton = new Button("Add");
          addButton.setOnAction(e -&gt; addButtonClicked());
          Button deleteButton = new Button("Delete");
          deleteButton.setOnAction(e -&gt; deleteButtonClicked());
  
          HBox hBox = new HBox();
          hBox.setPadding(new Insets(10,10,10,10));
          hBox.setSpacing(10);
          hBox.getChildren().addAll(symbolInput, nameInput, openingPriceInput, closingPriceInput, changeInPriceInput, addButton, deleteButton);
  
          table = new TableView&lt;&gt;();
          table.setItems(getStock());
          table.getColumns().addAll(symbolColumn, nameColumn, openingPriceColumn, closingPriceColumn, changeInPriceColumn);
  
          VBox vBox = new VBox();
          vBox.getChildren().addAll(table, hBox);
  
          Scene scene = new Scene(vBox);
          window.setScene(scene);
          window.show();
      }
  
      //Add button clicked
      public void addButtonClicked(){
          Stock Stock = new Stock();
          Stock.setSymbol(symbolInput.getText());
          Stock.setName(nameInput.getText());
          Stock.setOpeningPrice(Double.parseDouble(openingPriceInput.getText()));
          Stock.setClosingPrice(Double.parseDouble(closingPriceInput.getText()));
          Stock.setChangeInPrice(Double.parseDouble(changeInPriceInput.getText()));
          table.getItems().add(Stock);
          symbolInput.clear();
          nameInput.clear();
          openingPriceInput.clear();
          closingPriceInput.clear();
          changeInPriceInput.clear();
      }
  
      //Delete button clicked
      public void deleteButtonClicked(){
          ObservableList&lt;Stock&gt; StockSelected, allStocks;
          allStocks = table.getItems();
          StockSelected = table.getSelectionModel().getSelectedItems();
  
          StockSelected.forEach(allStocks::remove);
      }
  
      //Get all of the Stocks
      public ObservableList&lt;Stock&gt; getStock(){
          ObservableList&lt;Stock&gt; stocks = FXCollections.observableArrayList();
          stocks.add(new Stock("AMZN", "Amazon", 571, 576.4583, 5.4583));
          stocks.add(new Stock("EBAY", "eBay", 24.10, 23.7318 , -0.3682));
          stocks.add(new Stock("AAPL", "Apple Inc.", 103.91, 104.516, 0.606));
          stocks.add(new Stock("SNEJF", "Sony Corp", 24.375, 24.375, 0.00));
          stocks.add(new Stock("SBUX", "Starbucks", 58.32, 58.86, 0.54));
          return stocks;
      }
  
  
  }
  </code></pre>
  
  <p>Controller.java</p>
  
  <p>This contains the code for my login window.</p>
  
  <pre><code>import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  import javafx.stage.Stage;
  
  public class Controller {
      @FXML
      private Label labelStatus;
  
      @FXML
      private TextField textUsername;
  
      @FXML
      private TextField textPassword;
  
      public void Login(ActionEvent event) throws Exception {
          if (textUsername.getText().equals("CS1113") &amp;&amp; textPassword.getText().equals("Section011")) {
              labelStatus.setText("Login is Successful");
              Stage primaryStage = new Stage();
              Parent root = FXMLLoader.load(getClass().getResource("/sample/Main.fxml"));
              Scene scene = new Scene(root,400,400);
              primaryStage.setScene(scene);
              primaryStage.show();
          } else {
              labelStatus.setText("Login failed!!");
          }
      }
  
  }
  </code></pre>
  
  <p>The accompanying FXML file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.GridPane?&gt;
  &lt;?import javafx.scene.text.*?&gt;
  
  &lt;AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller"&gt;
    &lt;children&gt;
      &lt;Button fx:id="buttonLogin" layoutX="113.0" layoutY="224.0" mnemonicParsing="false" text="Login"&gt;
        &lt;font&gt;
          &lt;Font size="18.0" fx:id="x1" /&gt;
        &lt;/font&gt;
      &lt;/Button&gt;
      &lt;TextField fx:id="textUsername" layoutX="50.0" layoutY="68.0" prefWidth="200.0" promptText="username" /&gt;
      &lt;PasswordField id="txtPassword" fx:id="textPassword" layoutX="50.0" layoutY="150.0" prefWidth="200.0" promptText="password" /&gt;
      &lt;Label fx:id="labelStatus" font="$x1" layoutX="14.0" layoutY="14.0" prefWidth="272.0" text="Status" textAlignment="LEFT" textFill="#cc0000" /&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>I am sure there is an easier way to implement a large amount of my code or an easier way to implement some of the methods or functions I have created.</p>
  
  <p>Any help is greatly appreciated.</p>