1

I am currently using JavaFX and I want to reuse a method from another class. I have the method in a class called about and I want to reuse the method in a class called contact. Its a simple method, all it does is changes scene after the button is clicked. Code is below:

public void logoutButtonClick (ActionEvent actionEvent) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("FirstPage.fxml"));
    Stage window = (Stage) logoutButton.getScene().getWindow();
    window.setScene(new Scene(root, 800,500));

}
public void logoutButtonClick(ActionEvent actionEvent) throws IOException {

    aboutController aboutController = new aboutController();
    aboutController.logoutButtonClick();

}

At the moment I am getting an error:

java: method logoutButtonClick in class com.example.demo2.aboutController cannot be applied to given types;
  required: javafx.event.ActionEvent
  found:    no arguments
  reason: actual and formal argument lists differ in length

It is expecting something in the brackets but I am unsure what it is. Any help would be great. Thanks

Some more code of the classes:

public class contactController extends aboutController{

    @FXML
    public Button logoutButton;


    public void logoutButtonClick(ActionEvent actionEvent) throws IOException {

        aboutController aboutController = new aboutController();
        aboutController.logoutButtonClick(actionEvent);

    }
public class aboutController {

    @FXML
    public Button logoutButton;

    public void logoutButtonClick (ActionEvent actionEvent) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("FirstPage.fxml"));
    Stage window = (Stage) logoutButton.getScene().getWindow();
    window.setScene(new Scene(root, 800,500));

    }
sqlcoder
  • 27
  • 8
  • 2
    As an aside to what I state [here](https://stackoverflow.com/questions/76722561/how-do-i-reuse-a-method-from-another-class#comment135263123_76722588), standard Java naming conventions have class names following the `PascalCase` (i.e., `UpperCamelCase`) format. So, `aboutController` and `contactController` should be `AboutController` and `ContactController`, respectively. You should follow standard naming conventions when posting code to a public forum like Stack Overflow. – Slaw Jul 19 '23 at 15:57
  • 2
    Additionally, the way you have `ContactController` subclassing `AboutController` is strange. For one, you apparently want to use the implementation of `logoutButtonClick` from `AboutController`, yet you override that method in `ContactController`. Why override the method at all? And then you have an identical `logoutButton` field in both classes; the field in the subclass is _hiding_ the field in the superclass (note fields are not polymorphic). Since that field is public, you already have access to it in the subclass. Why define it "again"? – Slaw Jul 19 '23 at 16:01
  • 1
    As Slaw notes, likely your design should be changed, and possibly triggering a button click shouldn't be necessary. Usually, with a logout, a single action will logout the user in all places, and there is no need to generate multiple actions to have this occur. How to log out in all places though is application specific and beyond the scope of what I could answer here. To achieve it, likely you would apply elements of an [mvc](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) pattern, which is a more advanced (and complex) approach. – jewelsea Jul 19 '23 at 17:39
  • If you want to simulate a button click from code, you should call [`fire`](https://stackoverflow.com/q/29353790/1155209) on the button. – jewelsea Jul 19 '23 at 18:15

1 Answers1

0

Seeing as you're not using the actionEvent argument, I suggest creating a method in the aboutController class for logging out and calling that method from the Action Event method:

public class aboutController {

    @FXML
    public Button logoutButton;

    public void logoutButtonClick (ActionEvent actionEvent) throws IOException {
       logout();
    }

    public void logout(){
        Parent root = FXMLLoader.load(getClass().getResource("FirstPage.fxml"));
        Stage window = (Stage) logoutButton.getScene().getWindow();
        window.setScene(new Scene(root, 800,500));
    }

And then you can try calling the logout method from the contactController

public class contactController extends aboutController{

    @FXML
    public Button logoutButton; // If this is the same logout button as 
    //it's parent class, this is not necessary


    public void logoutButtonClick(ActionEvent actionEvent) throws IOException {

        super.logout();

    }

On a side note, as others have said, it's better to use Pascal Case when naming classes (i.e ContactController instead of contactController). Additionally, logoutButtonClick isn't the best name to give such a method since this is an action event not a click. I'd rather name it something like onActionLogoutButton.