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”

JavaFX Image Converter

Sometimes it is easier to grasp the concepts of a language or library such as JavaFX by seeing it in action. I’ve put together a JavaFX image converter application. This JavaFX app allows you to drag GIF’s, JPEG’s, BMP’s, or PNG’s onto the ImageView and then convert them and save them quickly as any of the other formats mentioned above.

JavaFX Image Converter
This JavaFX app allows quick conversion of BMP, GIF, JPG, and PNG files to any of the other formats and saving to your computer.

This JavaFX app is a double-clickable JAR file. That means that if you want to run it from the JAR file, just make sure you have Java installed and double-click the JavaFX JAR named image-converter.jar.

Continue reading “JavaFX Image Converter”

JavaFX Drag and Drop Images

If you deal with files in your JavaFX app, then you need to have drag and drop integration. You’ll be amazed at how easy drag-and-drop is in JavaFX.

The following JavaFX image drag-and-drop example only takes three files. You can just copy them into your JavaFX project, and run them if you’d like.

The resulting application allows you to drag an image file onto your window. As the image file moves over the window, it will change in a OS specific way, indicating that you can drop the file onto the open window. Once you drop the image file on to the window, the image will be displayed in the window. Continue reading “JavaFX Drag and Drop Images”

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”