No injectable field found in FXML Controller class for the id

97 Views Asked by At

I am very new to JavaFX and know nothing about it. I have been watching YouTube videos on how to use Scene Builder; but somehow when I'm on this part, I can't set the code and this is the message that prompts:

No injectable field found in FXML Controller class for the id LoginBtn

I need a fix for my problem that keeps on occurring.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="820.0" prefWidth="1080.0" style="-fx-background-color: #1D242A;" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
   <children>
      <Pane layoutX="315.0" layoutY="230.0" prefHeight="455.0" prefWidth="450.0" style="-fx-background-color: #2F353A;">
         <children>
            <Button fx:id="LoginBtn" layoutX="34.0" layoutY="397.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="382.0" style="-fx-background-color: #158BF8;" text="Login" textFill="WHITE">
               <font>
                  <Font name="System Bold" size="20.0" />
               </font>
            </Button>
            <Label fx:id="WelcomeBack" layoutX="129.0" layoutY="14.0" text="Welcome Back" textFill="WHITE">
               <font>
                  <Font name="System Bold" size="28.0" />
               </font>
            </Label>
            <Label fx:id="Sign_In" layoutX="197.0" layoutY="54.0" text="Sign In" textFill="#aeaeae">
               <font>
                  <Font name="System Bold" size="17.0" />
               </font>
            </Label>
            <Label fx:id="Password" layoutX="55.0" layoutY="246.0" text="Password" textFill="#aeaeae">
               <font>
                  <Font name="System Bold" size="17.0" />
               </font>
            </Label>
            <PasswordField layoutX="55.0" layoutY="280.0" prefHeight="33.0" prefWidth="339.0" promptText="Enter your password" />
            <TextField fx:id="txt_user" layoutX="55.0" layoutY="181.0" prefHeight="33.0" prefWidth="339.0" promptText="Enter you username" />
            <Label fx:id="Username" layoutX="53.0" layoutY="144.0" text="Username" textFill="#aeaeae">
               <font>
                  <Font name="System Bold" size="17.0" />
               </font>
            </Label>
         </children>
      </Pane>
      <Label fx:id="label_Budo" layoutX="343.0" layoutY="139.0" text="BUDO Enterprise Inc." textFill="WHITE">
         <font>
            <Font name="System Bold" size="40.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

and this one is the controller:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class LoginController implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }
}
1

There are 1 best solutions below

0
trashgod On

Your posted LoginController "has no injectable field…named for the id LoginBtn". As @jewelsea comments, SceneBuilder > View > Show Sample Controller Skeleton can produce the LoginController shown below. I placed your "Login.fxml" in a package named temp, using the fully qualified name:

<AnchorPane … fx:controller="temp.LoginController">

I added a simple "LoginApp.java", also shown below, to produce the image below. Note how "LoginApp.java" loads "Login.fxml", parsing it for a field that is named LoginBtn and annotated @FXML.

public class LoginController {

    @FXML
    private Button LoginBtn;
    …
}

Login

LoginController.java:

package temp;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class LoginController {

    @FXML
    private Button LoginBtn;

    @FXML
    private Label Password;

    @FXML
    private Label Username;

    @FXML
    private Label label_Budo;

    @FXML
    private TextField tfUser;

}

LoginApp.java

package temp;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/q/78052655/230513
 */
public class LoginApp extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        stage.setTitle("LoginApp");
        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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