2

I'm having trouble inputting my FXML file login into my JavaFX Application.

Can someone tell me what I am doing wrong?

Im very new to JavaFX from Swing so pardon if its really bad or incorrect.

package application;

import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane page = (AnchorPane) FXMLLoader.load(getClass().getResource("application/MainDisplay.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Welcome!");
            primaryStage.show();
        } 
        catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}

Here is the fXML File

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane prefHeight="400.0" prefWidth="600.0"     xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65">
   <children>
      <Text layoutX="177.0" layoutY="137.0" strokeType="OUTSIDE"     strokeWidth="0.0" text="Welcome!" wrappingWidth="140.02001953125">
         <font>
            <Font size="25.0" />
         </font>
         <cursor>
            <Cursor fx:constant="NONE" />
         </cursor>
      </Text>
      <Text layoutX="177.0" layoutY="179.0" strokeType="OUTSIDE"     strokeWidth="0.0" text="User Name:" wrappingWidth="85.9443359375" />
      <Text layoutX="177.0" layoutY="213.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password:" />
      <Button layoutX="353.0" layoutY="242.0" mnemonicParsing="false" text="Sign In" />
      <TextField layoutX="263.0" layoutY="161.0" />
      <PasswordField layoutX="263.0" layoutY="195.0" />
   </children>
</AnchorPane>
fabian
  • 80,457
  • 12
  • 86
  • 114
B Whitehead
  • 49
  • 2
  • 8
  • 3
    What trouble are you facing? – Tobias Sep 29 '16 at 20:45
  • it gives an error. it does not load the xml file and it crashes. – B Whitehead Sep 29 '16 at 20:56
  • 1
    In that case, please post the stack trace and any relevant information. We are here to help you, but you need to provide some very basic information. – Tobias Sep 29 '16 at 20:57
  • Please post the stacktrace of the error so that we can identify the problem. :) – David Landup Sep 29 '16 at 21:04
  • Are you getting *Location not set*? If a class from the `application` package is used to load a resource, you should either use `/application/MainDisplay.fxml` or just `MainDisplay.fxml` as path... – fabian Sep 29 '16 at 21:14

1 Answers1

0

Try this:

@Override
    public void start(Stage primaryStage){
        try {
            AnchorPane page = (AnchorPane) FXMLLoader.load(getClass().getResource("/application/MainDisplay.fxml"));
            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Welcome!");
            primaryStage.show();
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
Nevets17
  • 109
  • 1
  • 9