0

Firstly, I am aware that my question may take some flak because JavaFX has its own FileChooser (that is largely superior/preferable to Swing's JFileChooser) and that mixing FX and Swing should be dissuaded. However - I will explain the functionality I'm trying to build and I am very open to suggestions on how else get there - if indeed I'm going in the wrong direction.

So firstly - the main application is built in JavaFX and is designed to calculate and archive parameters for biological samples. The primary observable is a tabPane wherein a new instance of a tab is generated for each new sample. As well as calculating and archive parameters for each sample, the user is also be able to store any type of data file in relation to each sample within the project. This is just for archiving - I do not plan to have in depth interaction with these files. Now, I come from a UNIX background and would very much like a sensible data structure to my "Project Folder" which can be sharable, externally editable and human-readable. In this regard, my overall idea was to make my saved output as follows:


  • A root folder (i.e. not a bespoke file format - just a classic folder)

  • Within the root folder:

    1. A main XML file detailing specifics of the project

    2. A second tier of folders - one for each tab of the project


  • Within the sample-specific folders:

    1. An XML file detailing specifics of the sample

    2. All the imported files/data in relation to this sample


Now - that all being said, I have the open/save functionality I'm aiming for functioning with Swing's JFileChooser. With a customised FileView class underpinning the JFileChooser, I can define a specific type of folder (both in relation to a pseudo-extension and its internal files and their content) as untraversable, give it a specific icon and then open it with the filechooser, in effect, as if it were a true file.

However, with the JavaFX Choosers, this becomes complicated. Because the ShowSaveDialog method does not exist for DirectoryChooser, from having to use FileChooser.ShowSaveDialog(), you can easily incorrectly save a folder within an already existing project (instead of rewriting it) - as, at least in OSX, this is poorly highlighted in the system file explorer. A further pain is that in selecting a folder - it does not populate the savename input box, the explorer simply descends into that directory.

Likewise - in opening a folder, the FileChooser.ShowOpenDialog() is clearly inappropriate. Using DirectoryChooser.ShowDialog() here is much more palatable, however the distinction between normal folders and project related folders (i.e. setting them untraversable and defining an icon) is something still better handled in Swing's JFileChooser.

Therefore, is anyone able to recommend how I can open a JFileChooser from within my FX app? I've tried running JFileChooser from a SwingNode, but I don't think I've implemented it correctly.

    Dialog SaveDialog = new Dialog();
    SaveDialog.setTitle("Save Project");
    SaveDialog.setResizable(true);

    SwingNode swingnode = new SwingNode();

    JFileChooser SaveChooser = new JFileChooser();
    SaveChooser.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(".prot projects", "*.prot");
    SaveChooser.setFileFilter(filter);
    SaveChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    FileView view = new main.COBFileView();
    SaveChooser.setFileView(view);

    swingnode.setContent(SaveChooser);

    SaveDialog.getDialogPane().setContent(swingnode);
    Window window = SaveDialog.getDialogPane().getScene().getWindow();
    window.setOnCloseRequest(event -> window.hide());
    SaveDialog.showAndWait();    

Or, on the other hand how I can replicate this functionality using FX?

Any help would be greatly appreciated!

smokinjim
  • 5
  • 1
  • 1
    Swing and JavaFX use different threads, so you should at a minimum create and configure the `JFileChooser` on the Swing thread (while keeping the JavaFX code on the JavaFX Application Thread). It might end up being easier just to implement the file chooser functionality you want from scratch. – James_D May 21 '18 at 12:46
  • Apologies, but I'm still quite new to Java. Could you please explain the necessary code or provide a link regarding how I should go about configuring JFileChooser on the Swing thread? Thanks! – smokinjim May 21 '18 at 13:18
  • Use `SwingUtilities.invokeLater(...)`. The [`SwingNode` documentation](https://docs.oracle.com/javase/9/docs/api/javafx/embed/swing/SwingNode.html) shows you how to do this. – James_D May 21 '18 at 13:23

0 Answers0