从 ListView 拖放到 TreeView

Drag and Drop from ListView to TreeView(从 ListView 拖放到 TreeView)
本文介绍了从 ListView 拖放到 TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我只想将一个项目从我的 ListView 拖放到我的 TreeView 中的特定位置.我知道如何处理 DragEvents.但是我怎样才能得到我的 treeView 的放置位置呢?位置"是指在哪个树视图项目上下降(从该树视图项目获取参考)".

I just want to drag and drop an item from my ListView to a specific position in my TreeView. I know how to handle DragEvents. But how can I get the dropped position of my treeView? With "position" I mean "on which treeview-item dropped (get reference from this treeview-item)".

package main;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;



public class Program extends Application {
    String buffer = "";

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Tree View Sample");
        HBox box = new HBox();

        //ListView
        ListView<String> list = new ListView<String>();
        ObservableList<String> items = FXCollections.observableArrayList(
                "Single", "Double", "Suite", "Family App");
        list.setItems(items);
        list.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("Drag something from ListView");
            }
        });
        //TreeView
        TreeItem<String> rootItem = new TreeItem<String>("Inbox");
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<String>("Message" + i);
            rootItem.getChildren().add(item);
        }
        TreeView<String> treeView = new TreeView<String>(rootItem);
        treeView.setEditable(true);
        treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
            @Override
            public TreeCell<String> call(TreeView<String> stringTreeView) {
                TreeCell<String> treeCell = new TreeCell<String>() {
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);
                        }
                    }
                };

                treeCell.setOnDragDetected(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        System.out.println("Drag: " + treeCell.getItem());
                    }
                });

                treeCell.setOnDragDropped(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        //THIS METHOD DOESN'T WORK (It doesn't called, when i drag something from listview to treeview)
                        System.out.println("Dropped something");
                        event.setDropCompleted(true);
                        event.consume();
                    }
                });

                return treeCell;
            }
        });

        box.getChildren().addAll(list,treeView);
        //Create Scene
        Scene scene = new Scene(box);

        //Add Scene to Stage..
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

除了 setOnDragDetected 处理程序外,没有任何工作.为什么?
示例我正在尝试:
想要通过拖放消息 2 与双"进行更改.
或者我想用Single"更改消息 1.
上面的代码只是一个更大的项目的测试.

Nothing is working except the setOnDragDetected handler. Why?
Example what I'm trying:
Want to change via drag and drop Message 2 with "Double".
Or I want to change Message 1 with "Single".
The code above is just a test for a larger project.

推荐答案

你需要

  • startDragAndDrop() on DRAG_DETECTED 源事件
  • acceptTransferModes()DRAG_OVER 目标事件
  • event.getDragboard().getString()DRAG_DROPPED 目标事件
  • startDragAndDrop() on DRAG_DETECTED source event
  • acceptTransferModes() on DRAG_OVER target event
  • and event.getDragboard().getString() on DRAG_DROPPED target event

查看完整示例:

@Override
public void start( Stage primaryStage ) throws Exception
{
    HBox box = new HBox();

    // ListView
    ListView<String> list = new ListView<String>( FXCollections.observableArrayList( "Single", "Double", "Suite", "Family App" ) );

    list.setCellFactory( new Callback<ListView<String>, ListCell<String>>()
    {
        @Override
        public ListCell<String> call( ListView<String> param )
        {
            ListCell<String> listCell = new ListCell<String>()
            {
                @Override
                protected void updateItem( String item, boolean empty )
                {
                    super.updateItem( item, empty );
                    setText( item );
                }
            };

            listCell.setOnDragDetected( ( MouseEvent event ) ->
            {
                System.out.println( "listcell setOnDragDetected" );
                Dragboard db = listCell.startDragAndDrop( TransferMode.COPY );
                ClipboardContent content = new ClipboardContent();
                content.putString( listCell.getItem() );
                db.setContent( content );
                event.consume();
            } );

            return listCell;
        }
    } );

    //TreeView
    TreeItem<String> rootItem = new TreeItem<>( "Inbox" );
    rootItem.setExpanded( true );
    for ( int i = 1; i < 6; i++ )
    {
        TreeItem<String> item = new TreeItem<>( "Message " + i );
        rootItem.getChildren().add( item );
    }
    TreeView<String> treeView = new TreeView<>( rootItem );
    treeView.setEditable( true );

    treeView.setCellFactory( new Callback<TreeView<String>, TreeCell<String>>()
    {
        @Override
        public TreeCell<String> call( TreeView<String> stringTreeView )
        {
            TreeCell<String> treeCell = new TreeCell<String>()
            {
                @Override
                protected void updateItem( String item, boolean empty )
                {
                    super.updateItem( item, empty );
                    if ( item != null )
                    {
                        setText( item );
                    }
                }
            };

            treeCell.setOnDragEntered( ( DragEvent event ) ->
            {
                treeCell.setStyle( "-fx-background-color: aqua;" );
            } );

            treeCell.setOnDragExited( ( DragEvent event ) ->
            {
                treeCell.setStyle( "" );
            } );

            treeCell.setOnDragOver( ( DragEvent event ) ->
            {
                Dragboard db = event.getDragboard();
                if ( db.hasString() )
                {
                    event.acceptTransferModes( TransferMode.COPY_OR_MOVE );
                }
                event.consume();
            } );

            treeCell.setOnDragDropped( ( DragEvent event ) ->
            {
                System.out.println( "treeCell.setOnDragDropped" );
                Dragboard db = event.getDragboard();
                boolean success = false;
                if ( db.hasString() )
                {
                    System.out.println( "Dropped: " + db.getString() );
                    treeCell.getTreeItem().getChildren().add( new TreeItem<>( db.getString() ) );
                    treeCell.getTreeItem().setExpanded( true );
                    success = true;
                }
                event.setDropCompleted( success );
                event.consume();
            } );

            return treeCell;
        }
    } );

    box.getChildren().addAll( list, treeView );
    primaryStage.setScene( new Scene( box ) );
    primaryStage.show();
}

这篇关于从 ListView 拖放到 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)