Search code examples
javafxfxmlimporterror

Cannot import javafx fxml class


Using the FXML project template in netbeans, I've been experiencing an issue with defining the FX controller in my FXML file.

  • The main class, fxml document and fxml controller are all in the same package: "login".
  • I am importing the controller into the fxml document using it's fully qualified name.
  • Error occurs at the import line: "Class login.MyController does not exist."

FXML Document

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import login.MyController?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.MyController">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #091D34;" VBox.vgrow="ALWAYS">
      <children>
        <Polygon fill="#000000b1" layoutX="363.0" layoutY="103.0" points="-50.0, 40.0, 50.0, 40.0, 50.0, -73.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: #133863;" />
        <ButtonBar layoutX="233.0" layoutY="256.0" prefHeight="40.0" prefWidth="161.0">
          <buttons>
              <Button layoutX="135.0" layoutY="18.0" minWidth="49.0" mnemonicParsing="false" prefHeight="25.0" text="Login" onAction="#doLogin" />
              <Button layoutX="165.0" layoutY="18.0" mnemonicParsing="false" text="About" />
          </buttons>
        </ButtonBar>
        <PasswordField layoutX="186.0" layoutY="216.0" prefHeight="25.0" prefWidth="273.0" promptText="Password" onKeyPressed="#onEnter" />
        <TextField layoutX="186.0" layoutY="175.0" prefHeight="25.0" prefWidth="273.0" promptText="Username" />
        <Text layoutX="253.0" layoutY="76.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-fill: #236AB9;" text="Anatomy Law" textAlignment="CENTER" wrappingWidth="120.58984375">
           <font>
              <Font name="Aparajita" size="22.0" />
           </font>
        </Text>
  </children>
</AnchorPane>

Controller

package login;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class MyController implements Initializable {


@FXML
public void doLogin(Event e){
    System.out.println("do login called");
}

@FXML
public void onEnter(KeyEvent ke){
    if(ke.getCode() == KeyCode.ENTER){
        System.out.println("on enter called");
        login();
    }
}

private void login(){

}

@Override
public void initialize(URL location, ResourceBundle resources) {

}

}

Main

package login;

import java.net.URL;
import javafx.application.Application;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) throws Exception {    
    FXMLLoader fxmlLoader = new FXMLLoader();
    MyController fxmlController = new MyController();

    fxmlLoader.setController(fxmlController);
    fxmlLoader.setLocation(new URL("C:/.../login/Login.fxml"));

    VBox vbox = fxmlLoader.<VBox>load();
    Scene scene = new Scene(vbox);

    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

Note I recently tried the following line in my FXML document:

<?import login.*?> 

Which resulted in "package login does not exist."

And

<?import MyController?>

Which resulted in class does not exist.

So i'm assuming it's some kind of build path error but they're all in the same package. I even added the package that they're in to additional source packages to build from just to make sure but that didn't solve anything. My project layout is the following:

Project Name 
    Source Packages 
        login
            Login.fxml
            MyController.java
            Main.java 

I reviewed the other question postings for this problem but no one seemed to have this specific problem. If I did however miss someone's post with an identical answer, I will gladly take that reference.


Solution

  • Make sure that the document you are editing is definitely the document in your package. Somehow, in my case I was actually editing a document outside of the package with the same exact name. This is why it couldn't find the controller class when I tried to import it.