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”

JavaFX Fahrenheit to Celsius Converter in FXML

JavaFX Fahrenheit to Celsius Example
JavaFX Fahrenheit to Celsius example using FXML to layout the Scene on the Stage.

JavaFX is easy once you get the hang of it, but for new programmers that requires plenty of examples. More experienced programmers will find one or two examples are enough to master most of JavaFX, so a lot of web sites only put one or two examples up—enough for experienced programmers, but not nearly enough for newer programmers.

In this post, I present the classic temperature converter example modified to use JavaFX. This example uses basic MVC/MVP design. If you want to see this same JavaFX tutorial written in pure Java, look for my other tutorial on this site. It will be the one mentioning pure Java instead of FXML.

Continue reading “JavaFX Fahrenheit to Celsius Converter in FXML”

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.

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

    List<File> phil = board.getFiles();
    String path = phil

    if (path.endsWith(".txt")) {


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”