Open and Save Menus in JavaFX

I have already written a tutorial on JavaFX FileChoosers. However, the previous example did not integrate a JavaFX MenuBar or an FXML file into the mix. Typically, you don’t create or design FileChoosers in FXML, though some dialogs can be designed in FXML. However, FileChoosers tend to be created and shown programmatically, rather than in FXML for display later.

This JavaFX FileChooser example shows a basic MenuBar with Open, Save, SaveAs and Quit MenuItems as shown in the image below.

JavaFX FileChooser Menus
A simple JavaFX MenuBar with Open, Save, Save As and Quit MenuItems.

Continue reading “Open and Save Menus in JavaFX”

JavaFX File Chooser

The newest version of the JavaFX FileChooser uses native FileChoosers to allow the selection of files. This means that the FileChooser is guaranteed to pass Apple Store requirements on having the OS and not the app control file system operations. However, it also means that the look and feel of your app may clash with the look and feel of the JavaFX file chooser, thus providing a degraded user experience. Most would say that is a fair trade off, as customers will already be used to their OS’s file chooser, and custom a JavaFX file chooser might hurt the user experience.

This short JavaFX tutorial goes into the three most common FileChooser uses. They are:

  1. Choosing one file to open
  2. Choosing multiple files to open
  3. Saving to a file

We’ll go into all three uses with examples.

Continue reading “JavaFX File Chooser”

Recognizing Dragged File Types in JavaFX

Dragging files onto nodes in JavaFX is fairly easy. The main trick is making sure you are only accepting the correct file types. There are several approaches to determining file type on the fly, or on the drag over, in our case. However, I recommend just going with the simplest approach.

While you are dealing with the drag over event, get the Dragboard from the event. Then grab the List<File> from the JavaFX DragBoard. At that point it is simple to get the first file’s Path, convert it to a String, and check that it ends with an acceptable file extension.

No. Really. It’s simple.

Here’s the code for checking for a *.txt file being dragged on to a TextArea that is watching drag over events.

@FXML
void textDragOver(DragEvent event) {
  Dragboard board = event.getDragboard();
  if (board.hasFiles()) {

    List<File> phil = board.getFiles();
    String path = phil
        .get(0)
        .toPath()
        .toString();

    if (path.endsWith(".txt")) {
      event
      .acceptTransferModes(
          TransferMode.ANY);
    }        
  }
}

 

Continue reading “Recognizing Dragged File Types in JavaFX”

JavaFX Drop Plain Text Files

JavaFX drag and drop is easy. Modern apps require the ability to drop files when filling content, instead of opening the files in another application, copying the contents to the clipboard and then pasting the clipboard contents into the new app. Make sure to include drop capabilities in your JavaFX apps. It’s a good idea to have fully flushed out drop events included in your JavaFX applications from the start, or you’re likely to lose customers.

This tutorial shows how to include the ability to drag plain text files on to a JavaFX text area and have that JavaFX text area auto-populate with the contents of the files. Plain text files may or may not have the extension of *.txt. Plain text files  are not the same as rich text files that almost always have the extension of *.rtf.

Continue reading “JavaFX Drop Plain Text Files”