0

I was trying to make a simple JavaFX file. It contains the main class, a controller, and an XML file. I opened the XML file in the scene builder. Then added a button on the anchor pane. Gave the button an id called b1. After I added that that button to my controller class using @FXML, I see some errors in the XML file. It is denoted as -

Cannot set javafx.scene.control.Button to field 'b1

This is my main class

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

This is the controller class

package sample;

import javafx.fxml.FXML;

import java.awt.*;

public class Controller {

    @FXML
    private Button b1;
}

And this is the XML class where the verdict is located

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            ***<Button fx:id="b1" layoutX="68.0" layoutY="69.0" mnemonicParsing="false" text="Button" />***
         </children>
      </AnchorPane>
   </children>
</GridPane>

The start marked line is where the verdict is present. I have added the screenshot. Also, the verdict text is given at the beginning

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36

0 Answers0