Simple example of javafx.stage.DirectoryChooser |
java.io.File return from DirectoryChooser.showDialog() |
package javafx_directorychooser; import java.io.File; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.DirectoryChooser; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX_DirectoryChooser extends Application { @Override public void start(final Stage primaryStage) { final Label labelSelectedDirectory = new Label(); Button btnOpenDirectoryChooser = new Button(); btnOpenDirectoryChooser.setText("Open DirectoryChooser"); btnOpenDirectoryChooser.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { DirectoryChooser directoryChooser = new DirectoryChooser(); File selectedDirectory = directoryChooser.showDialog(primaryStage); if(selectedDirectory == null){ labelSelectedDirectory.setText("No Directory selected"); }else{ labelSelectedDirectory.setText(selectedDirectory.getAbsolutePath()); } } }); VBox vBox = new VBox(); vBox.getChildren().addAll( labelSelectedDirectory, btnOpenDirectoryChooser); StackPane root = new StackPane(); root.getChildren().add(vBox); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("http://java-buddy.blogspot.com/"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Related:
- FileChooser - Open File Dialog
Thanks for share!
ReplyDelete