Ask Your Question
2

How can a multi-column list view with support for drag and drop in JavaFX be created?

asked 2023-05-29 20:13:09 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-29 20:40:02 +0000

david gravatar image

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:

  1. Create the ListView control and add columns to 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);
  1. Enable drag and drop on the ListView:
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 ;
});
  1. Add items to the ListView:
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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-29 20:13:09 +0000

Seen: 7 times

Last updated: May 29 '23