如何在JavaFX中更改子项的顺序 [英] How to change order of children in JavaFX

查看:133
本文介绍了如何在JavaFX中更改子项的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JavaFX2子列表中更改节点的顺序?我尝试了 set() Collections.swap()然而两者都抛出 IllegalArgumentException in Parent 在某些时候,子列表在两个位置包含相同的项目(当节点处于新位置且尚未从旧位置移除时) 。在Parent中有一些标志,JavaFX在 toFront() toBront()内部使用,这可以防止异常,但是无法从外部访问它们。

Is it possible to change order of Nodes in JavaFX2 children list? I tried set() and Collections.swap() however both throw IllegalArgumentException in Parent as in some point the children list contains the same item at two positions (when node is at new position and has not been removed from the old position). There are flags inside Parent which JavaFX uses internaly in toFront() and toBack() which prevents the exception, however there is no way to access them from outside.

java.lang.IllegalArgumentException: Children: duplicate children added: parent = HBox@1424bf0
    at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
    at com.sun.javafx.collections.VetoableObservableList.set(VetoableObservableList.java:156)
    at com.sun.javafx.collections.ObservableListWrapper.set(ObservableListWrapper.java:281)
    at java.util.Collections.swap(Collections.java:532)


推荐答案

ObservableList<Node> workingCollection = FXCollections.observableArrayList(pane.getChildren());
Collections.swap(workingCollection, 0, 1);
pane.getChildren().setAll(workingCollection);

参考此代码:

package swapnode;

import java.util.Collection;
import java.util.Collections;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class SwapNode extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(20);
        /* Thid Part Swap Children of Node */
        Pane pane = view();
        ObservableList<Node> workingCollection = FXCollections.observableArrayList(pane.getChildren());
        Collections.swap(workingCollection, 0, 1);
        pane.getChildren().setAll(workingCollection);

        root.getChildren().addAll(view(),pane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public Pane view() {
        HBox pane = new HBox(10);
        Button button = new Button("Hello");
        TextField field = new TextField("World");
        pane.getChildren().addAll(button,field);
        return pane;
    }
}

这篇关于如何在JavaFX中更改子项的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆