Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
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.)
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.
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.
More from JavaWorld