chrisR chrisR - 17 days ago 8
Java Question

Java FX (FXML) setDisable

I have 2 buttons, one of them is Play, 2nd is Stop. If i click on Play he should be disabled and Stop should be enabled. I'm using Java FX ScreeBuilder 1.0 Why it doesn't work?

package javafxapplication1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.paint.Stop;
import javax.swing.JButton;
import javax.swing.plaf.basic.BasicOptionPaneUI;
import javafx.scene.control.Button;

/**
*
* @author Admin
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Button start;

@FXML
private Button stop;

@FXML
private void ButtonStart(ActionEvent event) {
start.setDisable(true);
stop.setDisable(false);
}

@FXML
private void ButtonStop(ActionEvent event) {
start.setDisable(false);
stop.setDisable(true);
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

Answer

Make sure that you have the Button entry in the corresponding .fxml file linked with the method in your controller.

In your case, these entries should look similar the following:

<Button fx:id="start" onAction="#ButtonStart" />
<Button fx:id="stop" onAction="#ButtonStop" />
Comments