Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Practical JavaFX 2, Part 3: Refactoring Swing JPad's advanced UI features

Migrate Swing JPad's dialogs, clipboard, and drag-and-drop to JavaFX

  • Print
  • Feedback

Page 5 of 6

JPadFX also needs to directly access the system clipboard so that it can determine when text is available for pasting. When text isn't available, Edit's Paste item must be disabled; otherwise, it's enabled.

The start() method executes the code to access the system clipboard and save its reference in a javafx.scene.input.Clipboard field variable:

cb = Clipboard.getSystemClipboard();

Before this menu is shown, Edit's event handler executes. Clipboard's hasString() method returns true when the system clipboard contains textual data:

miPaste.setDisable(!cb.hasString());

Each time the Edit menu is selected (and before the menu is shown), Clipboard's boolean hasString() method is executed on the system clipboard to determine whether the clipboard contains text. If the system clipboard contains text, true returns and the Paste menu item is enabled. If there is no text on the system clipboard then Paste is disabled. (Compare this code to Listing 13 in Part 1, which uses Swing's setEnabled() method to enable/disable a menu item. JavaFX provides a setDisable() method for this purpose.)

Drag and drop in JavaFX

JPadFX lets you drag files to and drop them on a given text area. Although multiple files can be dragged to and dropped on this control, only the first of these files is opened and its content displayed; the other files are ignored. Listing 7 shows how JPadFX supports drag and drop functionality.

Listing 7. JPadFX responds to drag events

EventHandler<DragEvent> ehde;
ehde = new EventHandler<DragEvent>()
{
   @Override
   public void handle(DragEvent de)
   {
      if (de.getDragboard().hasFiles())
         de.acceptTransferModes(TransferMode.COPY);
      de.consume();
   }
};
ta.setOnDragOver(ehde);
ehde = new EventHandler<DragEvent>()
{
   @Override
   public void handle(DragEvent de)
   {
      final Dragboard db = de.getDragboard();
      boolean success = false;
      if (db.hasFiles())
      {
         if (fDirty)
         {
            AreYouSure ays = new AreYouSure(stage, SAVE_CHNGS);
            EventHandler<ActionEvent> ehae1;
            ehae1 = new EventHandler<ActionEvent>()
            {
               @Override
               public void handle(ActionEvent ae)
               {
                  if (doSave())
                     doOpen(db.getFiles().get(0));
               }
            };
            ays.setOnYes(ehae1);
            EventHandler<ActionEvent> ehae2;
            ehae2 = new EventHandler<ActionEvent>()
            {
               @Override
               public void handle(ActionEvent ae)
               {
                  doOpen(db.getFiles().get(0));
               }
            };
            ays.setOnNo(ehae2);
            ays.show();
         }
         else
            doOpen(db.getFiles().get(0));
         success = true;
      }
      de.setDropCompleted(success);
      de.consume();
   }
};
ta.setOnDragDropped(ehde);

In Listing 7 we first create a javafx.scene.input.DragEvent-parameterized EventHandler instance that responds to "drag over" drag events. It registers this instance with the TextArea by invoking void setOnDragOver(EventHandler<? super DragEvent> value).

The event handler first obtains the javafx.scene.input.Dragboard instance associated with its DragEvent argument by calling DragEvent's Dragboard getDragboard() method. (Dragboard subclasses Clipboard, making dragboards drag-and-drop-specific clipboards.) It then invokes Dragboard's inherited boolean hasFiles() method to determine if a list of files have been dragged to the textarea.

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.
  • JavaWorld's Java Technology Insider is a podcast series that lets you learn from Java technology experts on your way to work.