Example usage for javafx.scene.control Button setOnAction

List of usage examples for javafx.scene.control Button setOnAction

Introduction

In this page you can find the example usage for javafx.scene.control Button setOnAction.

Prototype

public final void setOnAction(EventHandler<ActionEvent> value) 

Source Link

Usage

From source file:Main.java

@Override
public void start(final Stage primaryStage) {
    Group root = new Group();

    Button buttonLoad = new Button("Load");
    buttonLoad.setOnAction(new EventHandler<ActionEvent>() {
        @Override//from  w w  w .  ja v a2s.  c o  m
        public void handle(ActionEvent arg0) {
            FileChooser fileChooser = new FileChooser();
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)",
                    "*.txt");
            fileChooser.getExtensionFilters().add(extFilter);
            File file = fileChooser.showOpenDialog(primaryStage);
            System.out.println(file);
        }
    });
    VBox vBox = VBoxBuilder.create().children(buttonLoad).build();
    root.getChildren().add(vBox);
    primaryStage.setScene(new Scene(root, 500, 400));
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    String imagePath = "resources/picture/yourImage.jpg";
    Image image = new Image(imagePath);

    ImageView imageView = new ImageView(image);

    Button saveBtn = new Button("Save Image");
    saveBtn.setOnAction(e -> saveToFile(image));

    VBox root = new VBox(10, imageView, saveBtn);
    Scene scene = new Scene(root);
    stage.setScene(scene);//from  w ww. j  a v  a  2  s.co  m
    stage.setTitle("");
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    Button btn = new Button();
    btn.setText("Hello World");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            System.out.println("Hello World");
        }//from  w  w  w  .ja  va 2 s .  c  om
    });
    root.getChildren().add(btn);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Button b1 = new Button("B1");
    Button b2 = new Button("B2");
    Button b3 = new Button("B3");
    Button visibleBtn = new Button("Make Invisible");

    visibleBtn.setOnAction(e -> b2.setVisible(!b2.isVisible()));

    visibleBtn.textProperty().bind(new When(b2.visibleProperty()).then("Invisible").otherwise("Visible"));

    b2.managedProperty().bind(b2.visibleProperty());

    HBox root = new HBox();
    root.getChildren().addAll(visibleBtn, b1, b2, b3);

    Scene scene = new Scene(root);
    stage.setScene(scene);/*  w  w w .j a v  a 2 s .  co m*/
    stage.setTitle("");
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {

    Button openURLButton = new Button("Go!");
    openURLButton.setOnAction(e -> {
        HostServices host = getHostServices();
        JSObject js = host.getWebContext();
        if (js == null) {
            Stage s = new Stage(StageStyle.UTILITY);
            s.initModality(Modality.WINDOW_MODAL);
            Label msgLabel = new Label("This is an FX alert!");
            Group root = new Group(msgLabel);
            Scene scene = new Scene(root);
            s.setScene(scene);//  w  w  w .ja  v a 2 s .  c o m
            s.setTitle("FX Alert");
            s.show();
        } else {
            js.eval("window.alert('This is a JavaScript alert!')");
        }
    });

    Scene scene = new Scene(openURLButton);
    stage.setScene(scene);
    stage.setTitle("Knowing the Host");
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World!");
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override//from w ww  .j a va  2  s .c o  m
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Label nameLbl = new Label("Enter your name:");
    TextField nameFld = new TextField();

    Label msg = new Label();

    Button sayHelloBtn = new Button("Say Hello");

    sayHelloBtn.setOnAction(e -> {
        String name = nameFld.getText();
        if (name.trim().length() > 0) {
            msg.setText("Hello " + name);
        } else {/*from  w ww.j av a 2 s  . c  om*/
            msg.setText("Hello there");
        }
    });

    VBox root = new VBox();

    root.setSpacing(5);

    root.getChildren().addAll(nameLbl, nameFld, msg, sayHelloBtn);

    Scene scene = new Scene(root, 350, 150);
    stage.setScene(scene);
    stage.setTitle("hi");
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override/*from  w w  w . j ava  2 s.co  m*/
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Button Sample");
    stage.setWidth(300);/* ww  w  . j ava2  s .c om*/
    stage.setHeight(190);

    VBox vbox = new VBox();
    vbox.setLayoutX(20);
    vbox.setLayoutY(20);

    Image image = new Image(getClass().getResourceAsStream("a.png"));
    Button button1 = new Button("Accept", new ImageView(image));
    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            System.out.println("Accepted");
        }
    });

    vbox.getChildren().add(button1);
    vbox.setSpacing(10);
    ((Group) scene.getRoot()).getChildren().add(vbox);

    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    VBox root = new VBox(5);

    Label textLbl = new Label("Text:");
    TextArea text = new TextArea();
    text.setPrefRowCount(10);/*  w  w  w .  j a va2s  .co  m*/
    text.setPrefColumnCount(20);
    text.setWrapText(true);

    // Button to print the TextArea node
    Button printTextBtn = new Button("Print Text");
    printTextBtn.setOnAction(e -> print(text));

    // Button to print the entire scene
    Button printSceneBtn = new Button("Print Scene");
    printSceneBtn.setOnAction(e -> print(root));

    HBox buttonBox = new HBox(5, printTextBtn, printSceneBtn);

    root.getChildren().addAll(textLbl, text, buttonBox);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Printing Nodes");
    stage.show();
}