从ListView拖放到TreeView

时间:2015-05-26 08:54:11

标签: java javafx drag-and-drop javafx-8

我只是想将项目从ListView拖放到TreeView中的特定位置。我知道如何处理DragEvent。但是如何才能获得treeView的删除位置?随着"位置"我的意思是"在哪个treeview-item上删除了(从这个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来改变&#34; Double&#34; 或者我想用&#34; Single&#34;来改变消息1 上面的代码只是对更大项目的测试。

1 个答案:

答案 0 :(得分:5)

你需要

    startDragAndDrop()来源事件上
  • DRAG_DETECTEDacceptTransferModes()目标活动
  • DRAG_OVERevent.getDragboard().getString()目标活动
  • DRAG_DROPPED

见完整的例子:

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