Hello to all! The project on javafx springboot.

Made two tables one looks in a database, the second dynamic for the entities selected from base. Drag and drop should transfer selectable entities from the first table (database) to the second dynamic one. And then work with them. Whoever faced such tasks? Almost everything was done, but the dynamic table displays data in a geometrical progression. When selecting 1 row, it displays 2 rows and an increase. Dynamic table (here map-lu Entity from base to object for dynamic table):

private ObservableList<House> getHouseList(HousePrimaryRights housePrimaryRights) { List<House> newHouses = new ArrayList<>(); housesAllDinList = FXCollections.observableArrayList(newHouses); tableViewHouseUpdata = new TableView<>(housesAllDinList); if (housePrimaryRights != null) { IMergingContext context = new MergingContext(); House house = context.map(housePrimaryRights, House.class); newHouses.add(house); } else tableViewHouseUpdata.getItems().clear(); return housesAllDinList; 

Here I work to capture the desired rows in the table from the database.

 tableViewHouseData.setOnMouseClicked(event -> { if (event.getClickCount() == 1) { tableViewHouseData.getSelectionModel().selectedItemProperty().addListener( (observable, oldValue, newValue) -> showHouseDetails(newValue)); buttonAdd.fire(); } }); 

    1 answer 1

    A simple example of a one-way dragAndDrop (otherwise read about methods with the word drag):

     import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.input.ClipboardContent; import javafx.scene.input.DataFormat; import javafx.scene.input.Dragboard; import javafx.scene.input.TransferMode; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.io.Serializable; public class Main extends Application { @Override public void start(Stage primaryStage) { // Таблица источник TableView<MyEntity> tableView1 = new TableView<>(); TableColumn<MyEntity, Long> columnId1 = new TableColumn<>("Id"); columnId1.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().id)); TableColumn<MyEntity, String> columnName1 = new TableColumn<>("Name"); columnName1.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().name)); // Начало пертаскивания tableView1.setOnDragDetected(event -> { TableView<MyEntity> tableView = (TableView) event.getSource(); MyEntity selectedItem = tableView.getSelectionModel().getSelectedItem(); if ( selectedItem != null ) { ClipboardContent content = new ClipboardContent(); content.put(MyEntity.CLIPBOARD_DATA_FORMAT, selectedItem); tableView.startDragAndDrop(TransferMode.ANY).setContent(content); } event.consume(); }); tableView1.getColumns().addAll(columnId1, columnName1); tableView1.getItems().addAll( new MyEntity(1L, "One"), new MyEntity(2L, "Two"), new MyEntity(3L, "Three") ); // Таблица приемник TableView<MyEntity> tableView2 = new TableView<>(); TableColumn<MyEntity, Long> columnId2 = new TableColumn<>("Id"); columnId2.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().id)); TableColumn<MyEntity, String> columnName2 = new TableColumn<>("Name"); columnName2.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().name)); // Перетаскиваем с зажатой мышкой (до бросания) tableView2.setOnDragOver(event -> { if (event.getGestureSource() != event.getSource() && event.getDragboard().hasContent(MyEntity.CLIPBOARD_DATA_FORMAT) ) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } event.consume(); }); // Бросание объекта tableView2.setOnDragDropped(event -> { TableView tableView = (TableView) event.getSource(); Dragboard db = event.getDragboard(); boolean completed = false; if ( db.hasContent(MyEntity.CLIPBOARD_DATA_FORMAT) ) { MyEntity content = (MyEntity) db.getContent(MyEntity.CLIPBOARD_DATA_FORMAT); tableView.getItems().add(content); completed = true; } // Это для случая, если будет добавлена обработка dragDone на первой таблице event.setDropCompleted(completed); event.consume(); }); tableView2.getColumns().addAll(columnId2, columnName2); primaryStage.setScene(new Scene(new HBox(tableView1, tableView2), 300, 300)); primaryStage.show(); } // Сущность базы данных static class MyEntity implements Serializable { static DataFormat CLIPBOARD_DATA_FORMAT = new DataFormat(MyEntity.class.getName()); MyEntity(Long id, String name) { this.id = id; this.name = name; } Long id; String name; } } 
    • Thank you very much for what you need :) - Dmitry Grushetsky