0

I have my UIController class and a ButtonMethods class. The ButtonMethods class contains all the code for the button actions. I need to be able to use the buttons from the controller class in the ButtonMethods class. For example, I have these defined in the controller class

ButtonMethods button = new ButtonMethods();
@FXML Button buttonLockdown;
@FXML Button buttonRelease;

And for example, the buttonLockdown has an ActionEvent when clicked

@FXML
private void actionLockdown(ActionEvent event) {
    button.lockdown();

Ideally, I want the ButtonMethods to do this:

public void lockdown() {
    buttonLockdown.setDisable(true);
    onLockdown = true; 
    buttonRelease.setDisable(false);

I can't just put that code into the action event for various reasons, and putting the button objects into the parameters would get too messy with what I'm trying to do. So how can I get FXML objects into the button class?

1 Answer 1

3

Try to send the UIController itself as parameter:

private UIController thisController;

@Override
public void initialize(URL url, ResourceBundle rb) {
    thisController = this;
}

@FXML
private void actionLockdown(ActionEvent event) {
    button.lockdown(thisController);

then

public void lockdown(UIController controller) {
    controller.getButtonLockdown().setDisable(true);
    onLockdown = true; 
    controller.getButtonRelease().setDisable(false);

You can also use bindings in appropriate situations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.