Skip to content

Instantly share code, notes, and snippets.

@aoetk
Created April 17, 2012 16:55
Show Gist options
  • Save aoetk/2407472 to your computer and use it in GitHub Desktop.
Save aoetk/2407472 to your computer and use it in GitHub Desktop.
外部からドラッグアンドドロップしたファイルの情報をテーブルに表示するサンプル
package filedndsample;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class DroppedFile {
private StringProperty fileName = new SimpleStringProperty();
private StringProperty path = new SimpleStringProperty();
public StringProperty fileNameProperty() {
return fileName;
}
public StringProperty pathProperty() {
return path;
}
}
package filedndsample;
import java.io.File;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* ドラッグアンドドロップしたファイルをテーブルに表示するサンプル。
*/
public class FileDnDSample extends Application {
private ObservableList<DroppedFile> fileList = FXCollections.<DroppedFile>observableArrayList();
private TableView<DroppedFile> table;
private Scene scene;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("File DnD Sample");
final BorderPane root = new BorderPane();
table = new TableView<>();
table.setItems(fileList);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
final TableColumn<DroppedFile, String> fileNameCol = new TableColumn<>("Name");
fileNameCol.setCellValueFactory(new PropertyValueFactory<DroppedFile, String>("fileName"));
final TableColumn<DroppedFile, String> pathCol = new TableColumn<>("Path");
pathCol.setCellValueFactory(new PropertyValueFactory<DroppedFile, String>("path"));
table.getColumns().setAll(fileNameCol, pathCol);
root.setCenter(table);
scene = new Scene(root, 600, 300);
setEventHandler();
primaryStage.setScene(scene);
primaryStage.show();
}
private void setEventHandler() {
table.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
}
});
table.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasFiles()) {
for (File file : db.getFiles()) {
DroppedFile droppedFile = new DroppedFile();
droppedFile.fileNameProperty().set(file.getName());
droppedFile.pathProperty().set(file.getAbsolutePath());
fileList.add(droppedFile);
}
success = true;
}
event.setDropCompleted(success);
event.consume();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment