6

It's about JavaFX. When i want to inject fx:id in Scene Builder, i get this warning:
No injectable field found in FXML Controller class for the id 'something'.
I wanted to ignore it, and created a function, but it didn't work either. I created mainController class and added it into my FXML file. Here are my codes...

mainController.java

package main;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Slider;

public class mainController implements Initializable {

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }

    @FXML
    private ProgressBar pb;
    @FXML
    private Slider sl;
        @FXML
        private Label label;

    public void changed(ActionEvent event){

        }
}


Main.java

package main;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/main.fxml"));
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add("/fxml/styles/main.css");

        ProgressBar pb1 = new ProgressBar();
        ProgressBar pb2 = new ProgressBar();

        //pb1.

        //primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Something");
        primaryStage.show();
    }

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

}


main.fxml

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

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="main.mainController">
   <children>
      <BorderPane layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <top>
            <ProgressBar fx:id="pb" prefWidth="200.0" progress="0.0" BorderPane.alignment="CENTER">
               <BorderPane.margin>
                  <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
               </BorderPane.margin>
            </ProgressBar>
         </top>
         <bottom>
            <Slider fx:id="sl" onDragDetected="#changed" BorderPane.alignment="CENTER">
               <BorderPane.margin>
                  <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
               </BorderPane.margin>
            </Slider>
         </bottom>
      </BorderPane>
   </children>
</AnchorPane>

I did same things in my old projects, and they work like a charm. But this one seems not to obey me. Thanks in advance...

rzaaeeff
  • 850
  • 1
  • 10
  • 18
  • Could you provide the stacktrace? And what about the label? You inject one with the id `label` yet I can't find a label in your FXML. And you could write your `change`-method like: `@FXML private void change() {...}` – haisi Nov 11 '14 at 14:24
  • Thanks for your answer! You're absolutely right, i'm sorry that i've pasted old code here of my FXML in which i hadn't added label, but i've pasted my new controller class code. Another answer solved my question. Thank you! – rzaaeeff Nov 11 '14 at 15:06

2 Answers2

15

In Scene Builder, if the FXML file is associated to a controller class, you know that what makes the connection between the variable in the controller class (pb) and the object in the FXML file (<ProgressBar ... />) is the value of the object's fx:id.

So when you set an fx:id on an object, Scene Builder tries to parse the controller class trying to find a variable of that name.

If it doesn't find any, it displays this warning. It's just a reminder that you may want to add such a variable to the controller class, or that you have to choose other valid name from a list.

Since you have label defined on your controller, if you try to add a Label on Scene Builder, you can get its fx:id from the list:

Scene Builder

But if you assing another name, you will get the warning:

Scene Builder warning

On a side note, you don't need to instantiate the progress bar on the main class, since it will be instantiated in the controller. And ìf you try to link change(ActionEvent event) to a method in Scene Builder (#change), you have to annotate it with @FXML. Anyway, don't use onDragDetected with the slider.

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Adding @FXML before my procedure "changed" worked! Thank you so much! :) Which event do u prefer to use with slider? Thanks in advance... – rzaaeeff Nov 11 '14 at 15:02
  • You don't really need any from those listed on Scene Builder. You can track changes on the slider by adding a listener to its value property, inside `initialize`: `sl.valueProperty().addListener((obs,ov,nv)->System.out.println(nv));` – José Pereda Nov 11 '14 at 15:06
  • I'll try it as soon as i get home. Thanks a lot! – rzaaeeff Nov 11 '14 at 15:08
3

don't forget that Netbeans will Auto-Generate the code in the controller class.

Goto your FXMLDocument.fxml in your Netbeans project, and right click and select: "Make Controller".

Also, remember to save your changes first to your FXMLDocument.fxml, in Scene builder.

Mike Dever
  • 121
  • 1
  • 1
  • 6
    This was helpful. For other IDEs that may not have this feature you can go to Scene Builder and click View > Show Sample Controller Skeleton as well. Then copy that information over to your controller manually. – Terry Carter Feb 17 '17 at 00:14
  • @Terry Carter That's such a valuable piece of info I think it should be made into an answer... using IntelliJ and I was just beginning to assume this all had to be typed manually! – mike rodent Mar 29 '20 at 11:33