Sunday, March 24, 2013

JavaFX: simple example of DirectoryChooser

javafx.stage.DirectoryChooser provides support for standard directory chooser dialogs. On some platforms where file access may be restricted or not part of the user model (for example, on some mobile devices), opening a directory dialog may always result in a no-op (that is, null file being returned).

Simple example of javafx.stage.DirectoryChooser
Simple example of javafx.stage.DirectoryChooser
java.io.File return from DirectoryChooser.showDialog()
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


No comments:

Post a Comment