How to change order of children in JavaFX

前端 未结 2 1214
无人共我
无人共我 2021-01-04 09:32

Is it possible to change order of Nodes in JavaFX2 children list? I tried set() and Collections.swap() however both throw IllegalArgumentExce

相关标签:
2条回答
  • 2021-01-04 09:55
    ObservableList<Node> workingCollection = FXCollections.observableArrayList(pane.getChildren());
    Collections.swap(workingCollection, 0, 1);
    pane.getChildren().setAll(workingCollection);
    

    Refer to this code:

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 09:56

    You can move the child in the parent's children list by

    childNode.toFront();
    childNode.toBack();
    
    0 讨论(0)
提交回复
热议问题