Jump to content

JavaFX error: No controller specified

Merkey

Hi, so I'm trying to make one of my first JavaFX apps - a simple calculator taking 2 numbers and adding, subtracting them etc. But when I try to run it, it says "Exception in Application start method, no controller specified". Some people on Stack overflow suggested specifying the controller in fxml file, which didn't help either. Any other suggestions?

 

Main.java

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("TestStage");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;

import java.awt.*;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable{
    @FXML
    private TextField num1TextField;
    @FXML
    private TextField num2TextField;
    @FXML
    private Label resultLabel;
    @FXML
    private Button calculateButton;
    @FXML
    private ComboBox operationCombobox;
    @FXML
    private void calculateButtonClick(){
        double number1=Double.parseDouble(num1TextField.getText());
        double number2=Double.parseDouble(num2TextField.getText());
        double result=0;
        boolean exc=false;
        String operation=(String) operationCombobox.getSelectionModel().getSelectedItem();
        switch (operation){
            case "+":
                result=number1+number2;
                break;
            case "-":
                result=number1-number2;
                break;
            case "*":
                result=number1*number2;
                break;
            case "/":
                if (number2!=0){
                    result=number1/number2;
                }else {
                    exc=true;
                }
                break;
        }
        if (!exc){
            resultLabel.setText(String.valueOf(result));
        }else {
            resultLabel.setText("Nelze delit nulou");
        }
    }
    @Override
    public void initialize(URL url, ResourceBundle rb){
        ObservableList<String> operace= FXCollections.observableArrayList("+","-","*","/");
        operationCombobox.setItems(operace);
        operationCombobox.getSelectionModel().selectFirst();
    }
}

sample.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="167.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <TextField fx:id="cislo1TextField" layoutX="21.0" layoutY="71.0" prefHeight="25.0" prefWidth="120.0" />
      <TextField fx:id="cislo2TextField" layoutX="209.0" layoutY="71.0" prefHeight="25.0" prefWidth="120.0" />
      <ComboBox fx:id="operaceCombobox" layoutX="151.0" layoutY="71.0" prefHeight="25.0" prefWidth="47.0" />
      <Label layoutX="337.0" layoutY="75.0" text="=" />
      <Label fx:id="vysledekLabel" layoutX="373.0" layoutY="75.0" text="Label" />
      <Button fx:id="calculateButton" layoutX="222.0" layoutY="109.0" mnemonicParsing="false" onMouseClicked="#calculateButtonClick" text="Calculate" />
   </children>
</AnchorPane>

 

Link to comment
Share on other sites

Link to post
Share on other sites

you missed a ;

 

 

Please quote me or @ me in your response so I get a notification.                                                                       I really really really really like small text.

Occupation: Gamer | Hours: 24/7 Full-Time | Hourly Pay: 0 | Benefits: Chick Magnet

 

 

 

FINISHED BUILD - VIEW PROFILE

 

 

 

If you say I'm not always right, but I am, I will say I am right. 

rekt

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Merkey said:

Did I? Where?

There;  ^

 

:D 

Please quote me or @ me in your response so I get a notification.                                                                       I really really really really like small text.

Occupation: Gamer | Hours: 24/7 Full-Time | Hourly Pay: 0 | Benefits: Chick Magnet

 

 

 

FINISHED BUILD - VIEW PROFILE

 

 

 

If you say I'm not always right, but I am, I will say I am right. 

rekt

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Java hates you xD

 

Can't remember the last time I used javafx. Think I made an encyrptiom software with it when I did. There was a software I used for generating javafx designer form aka fxml. It makes life so much easier. That spared me frin coding stupid positions and size into the code itself. Try it out. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, wasab said:

Java hates you xD

 

Can't remember the last time I used javafx. Think I made an encyrptiom software with it when I did. There was a software I used for generating javafx designer form aka fxml. It makes life so much easier. That spared me frin coding stupid positions and size into the code itself. Try it out. 

The scene builder is built into the IDE (InteliJ Idea), which then generates the code. I didn't write the fxml code by myself, I just added "fx:controller="sample.Controller"". But it seems like the more I try to fix it, the more problems I have with it...

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, Merkey said:

The scene builder is built into the IDE (InteliJ Idea), which then generates the code. I didn't write the fxml code by myself, I just added "fx:controller="sample.Controller"". But it seems like the more I try to fix it, the more problems I have with it...

Yeah? I used eclipse. Let me look at my old project. You might be using the controller wrong.  

 

Edit: okay. Have you loaded your FXML file? I don't know how you did it but what I did was this. 

 

https://github.com/GAO23/Chain_Encryption/blob/master/Java_Version/Chain Encyprtion Java /src/Application/ui/mainWindow.java

 

I then simply use @FXML to get reference to UI elements in the FXML

 

https://github.com/GAO23/Chain_Encryption/blob/master/Java_Version/Chain Encyprtion Java /src/Application/logic/Form_Controller/mainSceneController.java

 

Ignore my comments if they don't make sense. I normally use Java for Android so I like to compare stuffs to things I see on Android like getviewbyid.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×