-
Notifications
You must be signed in to change notification settings - Fork 128
File chooser
VisUI provides a file chooser widget.
Chooser provides quick access to user home and desktop directory, quick access to drives partitions and removable devices. User can add custom favorites that will appear bellow partitions list. Chooser is platform dependent and can be only used on desktop.
File chooser should be created once and reused, if you reuse it will load much faster and it will remember last directory that user browsed.
//chooser creation
fileChooser = new FileChooser(Mode.OPEN);
fileChooser.setSelectionMode(SelectionMode.DIRECTORIES);
fileChooser.setListener(new FileChooserAdapter() {
@Override
public void selected (FileHandle file) {
textField.setText(file.file().getAbsolutePath());
}
});
//button listener
selectFileButton.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
//displaying chooser with fade in animation
getStage().addActor(fileChooser.fadeIn());
}
});
Favorites must be persistent so they have to be saved to a preferences file. If not changed file chooser will use default that may cause sharing favorites with other applications using VisUI. To avoid that call:
FileChooser.setFavoritesPrefsName(String name)
Where name is your program package for example: com.seriouscompay.seriousprogram
When creating file chooser you must pass mode that it will use, there are two possibilities Mode.SAVE and Mode.OPEN. It changes chooser texts and behavior, it also displays overwrite warring messages when dialog is in SAVE mode.
The following selection modes are available: FILES, DIRECTORIES, FILES_AND_DIRECTORIES. Please note that if dialog is in DIRECTORIES mode files still will be displayed, if user tries to select file, error message will be showed. Default is SelectionMode.FILES.
Default folder that will chooser display after creation is user home directory, this can be changed by calling:
chooser.setDirectory(String)
chooser.setDirectory(File)
chooser.setDirectory(FileHandle)
String path will be tried to resolved using absolute FileHandle. Same for passing File (dir.getAbsolutePath() is called)
All chooser text can be changed by creating locale object (FileChooserLocale). Please note that Locale object must be passed during chooser creation and cannot be changed later. When not passed chooser is using default one.
Chooser allow to select mutiple files. It is disabled by default, to enable it call:
chooser.setMultiselectionEnabled(true)
To select multiple files you need to press a key on keyboard, default is Keys.CONTROL_LEFT, to change it call:
chooser.setMultiselectKey (int multiselectKey)
Where multiselectKey value comes from LibGdx Keys class.
Chooser will fire listener functions when a file (or files) is selected or when a file selection has been canceled.
Listener and its empty implementation: FileChooserAdapater
Listener can be set and changed by calling:
chooser.setListener(FileChooserListener listener)
Listener has two selected() methods:
public void selected (Array<FileHandle> files);
public void selected (FileHandle file);
It is because single or multiple files can be selected. Both of them are always called after file selection, you should choose what you need. If you need multiple files use first one, if user can only select one file use second one.
When user selects multiple files (multi selection have to be enabled first) and you will use selected(FileHandle), you will only get first file from selected files list.
File list showed in chooser can by filtered by setting a file filter. To set it call:
chooser.setFilter(FileFilter filter)
Default one filters hidden files, and files that cannot be read (when in OPEN mode) or files that cannot be written to (when in SAVE mode).
See README for VisUI introduction.