To create a multi-column list view with support for drag and drop in JavaFX, you can use the ListView control and implement its drag-and-drop functionalities. Here is an example of how to do it:
ListView<String> listView = new ListView<>();
listView.setPrefWidth(400);
TableColumn<String, String> col1 = new TableColumn<>("Column 1");
col1.setCellValueFactory(data -> new SimpleStringProperty(data.getValue()));
TableColumn<String, String> col2 = new TableColumn<>("Column 2");
col2.setCellValueFactory(data -> new SimpleStringProperty(""));
listView.getColumns().addAll(col1, col2);
listView.setCellFactory(lv -> {
ListCell<String> cell = new ListCell<>();
cell.setOnDragDetected(event -> {
if (! cell.isEmpty()) {
Dragboard dragboard = cell.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(cell.getItem());
dragboard.setContent(content);
event.consume();
}
});
cell.setOnDragOver(event -> {
if (event.getGestureSource() != cell && event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
}
});
cell.setOnDragEntered(event -> {
if (event.getGestureSource() != cell && event.getDragboard().hasString()) {
// Highlight the cell to indicate that it can accept the drop
cell.setStyle("-fx-background-color: lightgray;");
}
});
cell.setOnDragExited(event -> {
// Remove highlight from the cell
cell.setStyle("");
});
cell.setOnDragDropped(event -> {
Dragboard dragboard = event.getDragboard();
boolean success = false;
if (dragboard.hasString()) {
// Move the item from its current position to the new position
int draggedIndex = listView.getItems().indexOf(dragboard.getString());
int dropIndex = cell.getIndex();
Collections.swap(listView.getItems(), draggedIndex, dropIndex);
success = true;
}
event.setDropCompleted(success);
event.consume();
});
return cell ;
});
ObservableList<String> items = FXCollections.observableArrayList();
items.addAll("Item 1", "Item 2", "Item 3", "Item 4", "Item 5");
listView.setItems(items);
With these steps, you should have a multi-column list view with drag and drop support in JavaFX.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-29 20:13:09 +0000
Seen: 7 times
Last updated: May 29 '23
How can set the Project Title in the Doxygen Configuration File?
How can I convert Double to Long in Java?
Can I add a default Parameter for a Method in Java like int calculate(int x, int y=2)?
How can the rejection of the class text_plain from JavaMail API due to a VerifyError be confirmed?
Is it feasible to utilize a Toggle Button to switch among multiple classes exceeding 2?
How can I set up Environment variables on my M1 MacBook and resolve the ZSH permission denied error?
What is the process for generating a dynamic subdomain/URL using vue.js?