本文介绍了如何用JavaFX中的VBox(通过拖动改变顺序)移动项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在VBox中拖动TitledPane。我在VBox中有n个Titlepane。我想在拖动时更改它们的顺序。我尝试使用一些MouseEvents和DragEvents。但它不适合我。



但是,我需要将Titledpane的索引移动到哪个位置。基于此,我需要在后端做一些事情。请帮帮我。



谢谢

解决方案

这适用于我...



  private static final String TAB_DRAG_KEY =sometpane; 
private ObjectProperty< TitledPane> draggingTab;
@Override
public void start(Stage primaryStage)throws Exception {
draggingTab = new SimpleObjectProperty< TitledPane>();
VBox vbox = new VBox(); (int i = 0; i final TitledPane pane = new TitledPane();

pane.setText(pane+(i + 1));
vbox.getChildren()。add(pane);
pane.setOnDragOver(new EventHandler< DragEvent>(){
@Override
public void handle(DragEvent event){
final Dragboard dragboard = event.getDragboard();
if(dragboard.hasString()
&& TAB_DRAG_KEY.equals(dragboard.getString())
&&&&&&  draggingTab.get()!= null){
事件.acceptTransferModes(TransferMode.MOVE);
event.consume();
}
}
});
pane.setOnDragDropped(new EventHandler< DragEvent>(){
public void handle(final DragEvent event){
Dragboard db = event.getDragboard();
boolean success = false ;
if(db.hasString()){
Pane parent =(Pane)pane.getParent();
对象源= event.getGestureSource();
int sourceIndex = parent.getChildren()。indexOf(source);
int targetIndex = parent.getChildren()。indexOf(pane);
列表< Node> nodes = new ArrayList&NodeB((parent.getChildren );
if(sourceIndex< targetIndex){
Collections.rotate(
nodes.subList(sourceIndex,targetIndex + 1),-1);
} else {
Collections.rotate(
nodes.subLis t(targetIndex,sourceIndex + 1),1);
}
parent.getChildren()。clear();
parent.getChildren()。addAll(nodes);
success = true;
}
event.setDropCompleted(success);
event.consume();
}
});
pane.setOnDragDetected(new EventHandler< MouseEvent>(){
@Override
public void handle(MouseEvent event){
Dragboard dragboard = pane.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString(TAB_DRAG_KEY);
dragboard.setContent(clipboardContent);
draggingTab.set(pane);
事件.consume();
}
});
}
TitledPane pane = new TitledPane(MAIN,vbox);
primaryStage.setScene(新场景(窗格,890,570));
primaryStage.show();
}


i want to drag TitledPane with in a VBox.I have n number of Titlepane's in a VBox. I want to change the order of them when dragded.I tried with some of MouseEvents and DragEvents. But its not working for me.

But i need the indexes of which Titledpane is moved to which place. Based on that i need to do something in backend. Please help me.

Thanks

解决方案

This works for me...

private static final String TAB_DRAG_KEY = "titledpane";
private ObjectProperty<TitledPane> draggingTab;
@Override
public void start(Stage primaryStage) throws Exception {
    draggingTab = new SimpleObjectProperty<TitledPane>();
    VBox vbox=new VBox();
    for(int i=0;i<4;i++) {
        final TitledPane pane=new TitledPane();
        pane.setText("pane"+(i+1));
        vbox.getChildren().add(pane);
        pane.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                final Dragboard dragboard = event.getDragboard();
                if (dragboard.hasString()
                        && TAB_DRAG_KEY.equals(dragboard.getString())
                        && draggingTab.get() != null) {
                    event.acceptTransferModes(TransferMode.MOVE);
                    event.consume();
                }
            }
        });
        pane.setOnDragDropped(new EventHandler<DragEvent>() {
            public void handle(final DragEvent event) {
                Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasString()) {
                    Pane parent = (Pane) pane.getParent();
                    Object source = event.getGestureSource();
                    int sourceIndex = parent.getChildren().indexOf(source);
                    int targetIndex = parent.getChildren().indexOf(pane);
                    List<Node> nodes = new ArrayList<Node>(parent.getChildren());
                    if (sourceIndex < targetIndex) {
                        Collections.rotate(
                                nodes.subList(sourceIndex, targetIndex + 1), -1);
                    } else {
                        Collections.rotate(
                                nodes.subList(targetIndex, sourceIndex + 1), 1);
                    }
                    parent.getChildren().clear();
                    parent.getChildren().addAll(nodes);
                    success = true;
                }
                event.setDropCompleted(success);
                event.consume();
            }
        });
        pane.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Dragboard dragboard = pane.startDragAndDrop(TransferMode.MOVE);
                ClipboardContent clipboardContent = new ClipboardContent();
                clipboardContent.putString(TAB_DRAG_KEY);
                dragboard.setContent(clipboardContent);
                draggingTab.set(pane);
                event.consume();
            }
        }); 
    }
    TitledPane pane=new TitledPane("MAIN",vbox);
    primaryStage.setScene(new Scene(pane, 890, 570));
    primaryStage.show();
}

这篇关于如何用JavaFX中的VBox(通过拖动改变顺序)移动项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 21:19