Home > OS >  Working with JavaFX and I got this error: Required type: java.awt.event.ActionEvent - Provided: java
Working with JavaFX and I got this error: Required type: java.awt.event.ActionEvent - Provided: java

Time:07-03

So, I'm trying to iron out a problem, but the suggestions from intellij are less than helpful.

As the title suggests: I am working with JavaFX and see this error message.

Intellij recommended migrate actionEvent type to 'ActionEvent'. After selecting to migrate the type nothing changed and the error persisted. Then, I tried the next recommendation from Intellij: change parameter 'actionEvent' type to 'ActionEvent' which brings up a window that indicates a "Super Method Found" and my options are to change the base method (which is the jdbc jar file) or current method.

As an alternative I tried changing the import statements (that are denoted with "this import statement") and eventually made my way back to the original error after intellij recommended I change override function to a lambda function.

package sample;

//import java.awt.event.ActionEvent; //this import statement
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent; //this import statement
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class LoggedInController implements Initializable{
    @FXML
    private Button button_logout;

    @FXML
    private Label label_welcome;

    @FXML
    private Label label_super_pw;

    @Override
    public void initialize(URL location, ResourceBundle resources){
        button_logout.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                //line below is where the error occurs
                DBUtils.changeScene(actionEvent, "sample.fxml", "Log In", null, null);
            }
        });
    }

    /**
     * This function displays the user's information once they log in
     * @param username the user's display name
     * @param super_pw the "super password" radio button that is selected when an account in created
     */
    public void setUserInformation(String username, String super_pw){
        label_welcome.setText("Welcome "   username   "!");
        label_super_pw.setText("Your super secret password is: "   super_pw   ".");
    }
}

CodePudding user response:

As you noted, the error happens here:

DBUtils.changeScene(actionEvent, "sample.fxml", "Log In", null, null);

And the error says the problem is with the actionEvent argument. More specifically, the type of actionEvent is javafx.event.ActionEvent (as it should be) but the changeScene method is defined to accept a java.awt.event.ActionEvent as the first argument. The former type is part of the JavaFX framework, whereas the latter type is from the AWT framework.

I assume DBUtils is your own class. And since you're writing a JavaFX-based application, not an AWT-based one, the solution is to go into your DBUtils source file and replace:

import java.awt.ActionEvent;

With:

import javafx.event.ActionEvent;

In general, unless you're mixing UI frameworks (which you should not be unless you really need to and know what you're doing), you should avoid any imports from AWT and Swing when writing a JavaFX application.

  • Related