1. Drag and Droop Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.File; import java.util.List; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.TransferMode; public class FxDrop { public static void setDropHandler(Node node, FxRunnable runnable) { node.setOnDragOver(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { if( event.getDragboard().hasFiles() ) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } event.consume(); } }); node.setOnDragDropped(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { Dragboard db = event.getDragboard(); boolean success = false; if( db.hasFiles() ) { success = true; List<File> files = db.getFiles(); runnable.run(files); } event.setDropCompleted(success); event.consume(); } }); } } |
2. Drag and Drop Files to Text Field
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class FXMLDocumentController implements Initializable { @FXML private TextField editText; @Override public void initialize(URL url, ResourceBundle rb) { } @FXML private void handleDrop(DragEvent event) { Dragboard db = event.getDragboard(); boolean success = false; if( db.hasFiles() ) { success = true; List<File> files = db.getFiles(); editText.setText( files.get(0).getPath() ); } event.setDropCompleted(success); event.consume(); } @FXML private void handleDragOver(DragEvent event) { Dragboard db = event.getDragboard(); if( db.hasFiles() ) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } event.consume(); } } |
Scene Builder Options:
댓글 없음:
댓글 쓰기