jeudi 21 mars 2019

Javafx listview with CheckBoxListCell doesn't work with Drag and Drop

I have a ListView with with a custom CheckBox Item called CheckBoxListItem. Everthing looks good but now I need to implement Drag and Drop for easier sorting.

I know that I have to use a custom CellFactory and set the Drag and Drop Events to the cell itself. My Problem is that I already have a CellFactory and dont know how to add the Events.

The commented code is what I think could be the way to do it but the updateItem method doesn't worked.


My main class:

public class Main extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        ListView<CheckBoxListItem> listView = new ListView<>();
        ScrollPane scrollPane = new ScrollPane(listView);
        scrollPane.setFitToHeight(true);
        scrollPane.setFitToWidth(true);

        for(int i=1; i<15; i++) {
            listView.getItems().add(new CheckBoxListItem("Value"+i));
        }

        listView.setCellFactory(CheckBoxListCell.forListView(CheckBoxListItem::selectedProperty, new StringConverter<    CheckBoxListItem>() {
            @Override
            public String toString(CheckBoxListItem object) {
                return object.getName();
            }

            @Override
            public CheckBoxListItem fromString(String string) {
                return null;
            }
        }));

        /*
        ObjectProperty<CheckBoxListCell<CheckBoxListItem>> dragSource = new SimpleObjectProperty<>();
        listView.setCellFactory(lv -> {
            CheckBoxListCell<CheckBoxListItem> cell = new CheckBoxListCell<CheckBoxListItem>(){
                @Override
                public void updateItem(CheckBoxListItem item , boolean empty) {
                    super.updateItem(item, empty);
                    if (item == null) {
                        setGraphic(null);
                    }else {
                        setGraphic(?????);
                    }
                }
            };

            cell.setOnDragDetected(event -> {
                //TODO
            });
            cell.setOnDragOver(event -> {
                //TODO
            });
            cell.setOnDragDropped(event -> {
                //TODO
            });
            return cell ;
        });
        */

        Scene scene = new Scene(scrollPane, 350, 450);
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void stop() {
        System.exit(0);
    }

}

and CheckBoxListItem.java:

public class CheckBoxListItem {
    private ReadOnlyStringWrapper name = new ReadOnlyStringWrapper();
    private BooleanProperty selected = new SimpleBooleanProperty(false);

    public CheckBoxListItem(String name) {
        this.name.set(name);
    }

    public CheckBoxListItem(String name, boolean selected) {
        this.name.set(name);
        this.selected.set(selected);
    }

    public String getName() {
        return name.get();
    }

    public BooleanProperty selectedProperty() {
        return selected;
    }

    public boolean isSelected() {
        return selected.get();
    }

    public void setSelected(boolean selected) {
        this.selected.set(selected);
    }
}




Aucun commentaire:

Enregistrer un commentaire