Created
January 1, 2018 02:40
-
-
Save jdesive/8a1d7fa380fbecae2ad524bc6210a687 to your computer and use it in GitHub Desktop.
Example of a JavaFX DirectoryChooser widget that can resize to fit the screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.desive.stages.components; | |
import com.desive.utilities.StageDictionary; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.stage.DirectoryChooser; | |
import javafx.stage.Stage; | |
import javax.swing.filechooser.FileSystemView; | |
import java.io.File; | |
/* | |
Created by Jack DeSive on 12/31/2017 at 6:30 PM | |
*/ | |
public class FolderSelector extends GridPane { | |
private TextField directoryPathField; | |
private Button selectDirectoryButton; | |
private Label groupLabel; | |
private Group buttonGroup = new Group(); | |
private DirectoryChooser directoryChooser = new DirectoryChooser(); | |
private File selectedDirectory = null; | |
private Stage windowOwner; | |
private double gridHGap = 10D, gridPadding = 10D; | |
public FolderSelector(String groupLabel, StageDictionary stageDictionary, Stage windowOwner) { | |
this.windowOwner = windowOwner; | |
setupDirectoryChooser(stageDictionary); | |
setupComponents(groupLabel, stageDictionary); | |
this.setHgap(gridHGap); | |
this.setPadding(new Insets(gridPadding)); | |
this.add(this.groupLabel, 0, 0, 2, 1); | |
this.add(directoryPathField, 0, 1); | |
this.buttonGroup.getChildren().add(selectDirectoryButton); | |
this.add(buttonGroup, 1, 1); | |
} | |
public File getSelectedDirectory() { | |
if(selectedDirectory != null && selectedDirectory.isDirectory()){ | |
return selectedDirectory; | |
} | |
return null; | |
} | |
public void resize() { | |
this.setPrefWidth(windowOwner.getWidth()); | |
setupColumnConstraints(); | |
} | |
private void setupColumnConstraints(){ | |
buttonGroup.applyCss(); | |
buttonGroup.layout(); | |
ColumnConstraints column1 = new ColumnConstraints(); | |
column1.setPrefWidth(this.getPrefWidth()-buttonGroup.getLayoutBounds().getWidth()); | |
this.getColumnConstraints().clear(); | |
this.getColumnConstraints().add(column1); | |
} | |
private void setupDirectoryChooser(StageDictionary stageDictionary) { | |
directoryChooser.setTitle(stageDictionary.SELECT_DIRECTORY_POPUP_TITLE); | |
directoryChooser.setInitialDirectory(FileSystemView.getFileSystemView().getHomeDirectory()); | |
} | |
private void setupComponents(String groupLabel, StageDictionary stageDictionary) { | |
directoryPathField = new TextField(); | |
selectDirectoryButton = new Button(stageDictionary.COM_DIRECTORY_SELECT_BUTTON_TEXT); | |
this.groupLabel = new Label(groupLabel); | |
selectDirectoryButton.setOnAction(event -> { | |
selectedDirectory = directoryChooser.showDialog(windowOwner); | |
if(selectedDirectory != null){ | |
directoryPathField.setText(selectedDirectory.getAbsolutePath()); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public void start(Stage primaryStage) throws Exception { | |
// StageDictionary is just my class for strings. (Can be replaced) | |
FolderSelector folderSelector = new FolderSelector("Select a Directory", new StageDictionary(), primaryStage); | |
GridPane grid = new GridPane(); | |
grid.getChildren().add(folderSelector, 0, 0); | |
Scene scene = new Scene(grid, 700, 800); | |
primaryStage.setScene(scene); | |
// Here is the magic. Add listeners so FolderSelector.class knows when to resize. | |
scene.widthProperty().addListener(event -> folderSelector.resize()); | |
scene.heightProperty().addListener(event -> folderSelector.resize()); | |
primaryStage.show(); | |
folderSelector.resize(); // Capture first layout (Must be after Stage.show()) | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment