2013-07-20 13 views
11

में बच्चों के क्रम को कैसे बदलें जावाफैक्स 2 बच्चों की सूची में नोड्स का ऑर्डर बदलना संभव है? मैंने set() और Collections.swap() की कोशिश की, हालांकि दोनों Parent में फेंकते हैं क्योंकि कुछ बिंदुओं में बच्चों की सूची में दो पदों पर एक ही आइटम होता है (जब नोड नई स्थिति में होता है और पुरानी स्थिति से हटा नहीं दिया जाता है)। माता-पिता के अंदर झंडे हैं जो JavaFX toFront() और toBack() में internaly का उपयोग करता है जो अपवाद को रोकता है, हालांकि बाहर से उन्हें एक्सेस करने का कोई तरीका नहीं है।जावाएफएक्स

java.lang.IllegalArgumentException: Children: duplicate children added: parent = [email protected] 
    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) 

उत्तर

7
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; 
    } 
} 
+0

प्लेटफॉर्म.रुनलाटर() का उपयोग करना सुनिश्चित करें यदि जावाएफएक्स थ्रेड के बाहर कोड निष्पादित करते हैं! – klonq

19

आप द्वारा

childNode.toFront(); 
childNode.toBack(); 
+1

तो toFront() या toBack() का उपयोग कर, आप हो सकता है कि दोनों ने कोशिश की हो क्योंकि मैंने HBox के भीतर एक बटन पर फ़्रंट() की कोशिश की थी और यह इसे HBox में लेबल के दाईं ओर रखा था। यह व्यवहार मेरे लिए counterintuitive था – amartin7211