Java - static method?

53 Views Asked by At


I am trying to understand the following JavaFX example. I do not understand why it is not necessary to create an object (at "openButton.setOnAction", at "handle", at "openFile") - because "openFile" is not a static method ?!

Thanks.

public final class FileChooserSample extends Application {

private Desktop desktop = Desktop.getDesktop();

@Override
public void start(final Stage stage) {
    stage.setTitle("File Chooser Sample");

    final FileChooser fileChooser = new FileChooser();

    final Button openButton = new Button("Open a Picture...");
    final Button openMultipleButton = new Button("Open Pictures...");

    openButton.setOnAction(
        new EventHandler<ActionEvent>() {
            @Override
            public void handle(final ActionEvent e) {
                File file = fileChooser.showOpenDialog(stage);
                if (file != null) {
                    openFile(file);
                }
            }
        });

    openMultipleButton.setOnAction(
        new EventHandler<ActionEvent>() {
            @Override
            public void handle(final ActionEvent e) {
                List<File> list =
                    fileChooser.showOpenMultipleDialog(stage);
                if (list != null) {
                    for (File file : list) {
                        openFile(file);
                    }
                }
            }
        });


    final GridPane inputGridPane = new GridPane();

    GridPane.setConstraints(openButton, 0, 0);
    GridPane.setConstraints(openMultipleButton, 1, 0);
    inputGridPane.setHgap(6);
    inputGridPane.setVgap(6);
    inputGridPane.getChildren().addAll(openButton, openMultipleButton);

    final Pane rootGroup = new VBox(12);
    rootGroup.getChildren().addAll(inputGridPane);
    rootGroup.setPadding(new Insets(12, 12, 12, 12));

    stage.setScene(new Scene(rootGroup));
    stage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}

private void openFile(File file) {
    try {
        desktop.open(file);
    } catch (IOException ex) {
        Logger.getLogger(
            FileChooserSample.class.getName()).log(
                Level.SEVERE, null, ex
            );
    }
}

}

0

There are 0 best solutions below