From: Debayan Sutradhar Date: Fri, 11 Jun 2021 11:12:35 +0530 Subject: Added DirectoryChooser and FileChooser with initial directory selector --- Added DirectoryChooser and FileChooser with initial directory selector --- --- /dev/null +++ b/src/main/java/com/stream_pi/util/uihelper/HBoxInputBoxWithDirectoryChooser.java @@ -0,0 +1,85 @@ +/* +Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad +Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel QuiƱones (SamuelQuinones) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +Originally Written by : Debayan Sutradhar (rnayabed) +*/ + +package com.stream_pi.util.uihelper; + +import javafx.geometry.Insets; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.TextField; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.stage.DirectoryChooser; +import org.kordamp.ikonli.javafx.FontIcon; + +import java.io.File; + +public class HBoxInputBoxWithDirectoryChooser extends HBox +{ + public HBoxInputBoxWithDirectoryChooser(String labelText, TextField textField, CheckBox enablerCheckBox) + { + this(labelText, textField, enablerCheckBox, 150, null); + } + + public HBoxInputBoxWithDirectoryChooser(String labelText, TextField textField, CheckBox enablerCheckBox, + File initialDirectory) + { + this(labelText, textField, enablerCheckBox, 150, initialDirectory); + } + + public HBoxInputBoxWithDirectoryChooser(String labelText, TextField textField, CheckBox enablerCheckBox, + int prefWidth, File initialDirectory) + { + textField.setDisable(true); + + HBoxInputBox hBoxInputBox = new HBoxInputBox(labelText, textField, prefWidth); + setHgrow(hBoxInputBox, Priority.ALWAYS); + getChildren().addAll(hBoxInputBox); + setSpacing(5.0); + + Button button = new Button(); + FontIcon fontIcon = new FontIcon("far-folder"); + button.setGraphic(fontIcon); + + button.setOnAction(event -> { + DirectoryChooser directoryChooser = new DirectoryChooser(); + + if(initialDirectory!=null) + { + directoryChooser.setInitialDirectory(initialDirectory); + } + + try + { + File selectedDirectory = directoryChooser.showDialog(button.getScene().getWindow()); + textField.setText(selectedDirectory.getAbsolutePath()); + } + catch (NullPointerException e) + { + // No folder selected + } + }); + + getChildren().add(button); + + if(enablerCheckBox!=null) + { + button.disableProperty().bind(enablerCheckBox.selectedProperty()); + HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45)); + getChildren().add(enablerCheckBox); + } + } +} --- 'a/src/main/java/com/stream_pi/util/uihelper/HBoxInputBoxWithFileChooser.java' +++ b/src/main/java/com/stream_pi/util/uihelper/HBoxInputBoxWithFileChooser.java @@ -32,11 +32,19 @@ public class HBoxInputBoxWithFileChooser public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox, FileChooser.ExtensionFilter... extensionFilters) { - this(labelText, textField, enablerCheckBox, 150, extensionFilters); + this(labelText, textField, enablerCheckBox, 150, null, extensionFilters); } public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox, - int prefWidth, FileChooser.ExtensionFilter... extensionFilter) + File initialDirectory, + FileChooser.ExtensionFilter... extensionFilters) + { + this(labelText, textField, enablerCheckBox, 150, initialDirectory, extensionFilters); + } + + public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox, + int prefWidth, File initialDirectory, + FileChooser.ExtensionFilter... extensionFilter) { textField.setDisable(true); @@ -52,6 +60,11 @@ public class HBoxInputBoxWithFileChooser button.setOnAction(event -> { FileChooser fileChooser = new FileChooser(); + if(initialDirectory!=null) + { + fileChooser.setInitialDirectory(initialDirectory); + } + if(extensionFilter!=null) { fileChooser.getExtensionFilters().addAll( @@ -59,9 +72,10 @@ public class HBoxInputBoxWithFileChooser ); } - try { - File selectedDirectory = fileChooser.showOpenDialog(button.getScene().getWindow()); - textField.setText(selectedDirectory.getAbsolutePath()); + try + { + File selectedFile = fileChooser.showOpenDialog(button.getScene().getWindow()); + textField.setText(selectedFile.getAbsolutePath()); } catch (NullPointerException e) {