public class ButtonDemo2 extends Application implements EventHandler
1: import javafx.application.Application;
2: import javafx.scene.Scene;
3: import javafx.stage.Stage;
4: import javafx.scene.layout.VBox;
5: import javafx.scene.control.Button;
6: import javafx.event.ActionEvent;
7: import javafx.event.EventHandler;
8:
9: /**
10: Demonstration of event handling within the ButtonDemo2 class.
11: */
12: public class ButtonDemo2 extends Application implements EventHandler<ActionEvent>
13: {
14: private Button btnSunny;
15: private Button btnCloudy;
16:
17: public static void main(String[] args)
18: {
19: launch(args);
20: }
21:
22: @Override
23: public void handle(ActionEvent event)
24: {
25: // This method can access the member variables
26: // which reference the other GUI controls
27: if (event.getSource() instanceof Button)
28: {
29: Button btnClicked = (Button) event.getSource();
30: if (btnClicked.getText().equals("Sunny"))
31: {
32: // Disable the cloudy button if sunny clicked
33: btnCloudy.setDisable(true);
34: }
35: else if (btnClicked.getText().equals("Cloudy"))
36: {
37: // Disable the sunny button if cloudy clicked
38: btnSunny.setDisable(true);
39: }
40: }
41: }
42:
43: @Override
44: public void start(Stage primaryStage) throws Exception
45: {
46: VBox root = new VBox();
47: btnSunny = new Button("Sunny");
48: btnCloudy = new Button("Cloudy");
49:
50: btnSunny.setOnAction(this);
51: btnCloudy.setOnAction(this);
52:
53: root.getChildren().add(btnSunny);
54: root.getChildren().add(btnCloudy);
55:
56: Scene scene = new Scene(root, 300, 100);
57: primaryStage.setTitle("Button Demo 2");
58: primaryStage.setScene(scene);
59: primaryStage.show();
60: }
61: }