【问题标题】:Javafx listview with CheckBoxListCell doesn't work with Drag and Drop带有 CheckBoxListCell 的 Javafx 列表视图不适用于拖放
【发布时间】:2019-08-12 12:36:45
【问题描述】:

我有一个ListView 和一个名为CheckBoxListItem 的自定义复选框项。一切看起来都不错,但现在我需要实现拖放以便于排序。

我知道我必须使用自定义 CellFactory 并将拖放事件设置为单元格本身。我的问题是我已经有一个CellFactory 并且不知道如何添加事件。

注释的代码是我认为可能的方法,但updateItem 方法不起作用。


我的主要课程:

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);
    }

}

和 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);
    }
}

【问题讨论】:

  • 我知道但没有复选框@Sedrick
  • 扩展 CheckBoxListCell 并在那里添加您的处理程序
  • @kleopatra 我试过了,但没有用。您能分享一个工作示例吗?

标签: java listview javafx checkbox drag-and-drop


【解决方案1】:

解决方案是您必须将两种方法结合起来。您只能在内部模仿 CheckBoxListCell.forListView() 所做的事情。即,使用 observableProperty 和转换器创建一个新的 CheckBoxListCell。我刚刚将您的演示修改为下面,它按预期工作。

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.stage.Stage;
import javafx.util.StringConverter;

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::selectedProperty, new StringConverter<CheckBoxListItem>() {
                @Override
                public String toString(CheckBoxListItem object) {
                    return object.getName();
                }

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

            cell.setOnDragDetected(event -> {
                System.out.println("Detected fired...");
            });
            cell.setOnDragOver(event -> {
                System.out.println("DragOver fired...");
            });
            cell.setOnDragDropped(event -> {
                System.out.println("DragDropped fired...");
            });
            return cell;
        });


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

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

}

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多