FX 2.x:如何删除SplitPane?

时间:2012-07-17 09:57:34

标签: button javafx-2 multisplitpane

我可以通过点击“添加窗格”按钮在XYChart中添加SplitPane,它可以正常工作。

现在,我想通过单击“删除窗格”按钮删除SplitPane。

这是完整的代码

public class SplitPaneFxTest extends Application {

SplitPane splitPane = new SplitPane();
double    percSplit = 0.50;
int       idxSplit  = 0;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(SplitPaneFxTest.class, args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("SplitPane Test");

    Group root = new Group();
    Scene scene = new Scene(root, 800, 650, Color.WHITE);

    //CREATE THE SPLITPANE        
    splitPane.setPrefSize(scene.getWidth(), scene.getHeight());
    splitPane.setOrientation(Orientation.VERTICAL);        

    //ADD LAYOUTS AND ASSIGN CONTAINED CONTROLS
    BorderPane upperPane = new BorderPane();
    HBox hbox = new HBox();

    Button button1 = new Button("Add Pane");
    hbox.getChildren().addAll(button1);
    upperPane.setTop(hbox);

    Button button2 = new Button("Remove Pane");
    hbox.getChildren().addAll(button2);
    upperPane.setTop(hbox);

    BorderPane lowerPane = new BorderPane();

    splitPane.getItems().addAll(upperPane);
    splitPane.setDividerPosition(idxSplit, 0.70);
    idxSplit++;

    splitPane.getItems().addAll(lowerPane);        
    idxSplit++;

    root.getChildren().add(splitPane);

    primaryStage.setScene(scene);
    primaryStage.show();

    button1.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {

        BorderPane myborderpane = new BorderPane();            
        splitPane.getItems().addAll(myborderpane);

        ObservableList<SplitPane.Divider> splitDiv =  splitPane.getDividers();

        System.out.println("splitDiv.size() "+splitDiv.size());

        percSplit = 1/(double)(splitDiv.size()+1);
        for (int i = 0; i< splitDiv.size(); i++) {                
            System.out.println("i "+i+" percSplit "+percSplit);
            splitPane.setDividerPosition(i, percSplit);
            percSplit += 1/(double)(splitDiv.size()+1);
        }
      }
    });
}
}

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:2)

您可以存储内部窗格列表,并根据此列表将其删除:

    final List<Pane> panes = new ArrayList<Pane>();

    button1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {

            BorderPane myborderpane = new BorderPane();

            //adding
            panes.add(myborderpane);

            splitPane.getItems().addAll(myborderpane);

            ObservableList<SplitPane.Divider> splitDiv = splitPane.getDividers();

            System.out.println("splitDiv.size() " + splitDiv.size());

            percSplit = 1 / (double) (splitDiv.size() + 1);
            for (int i = 0; i < splitDiv.size(); i++) {
                System.out.println("i " + i + " percSplit " + percSplit);
                splitPane.setDividerPosition(i, percSplit);
                percSplit += 1 / (double) (splitDiv.size() + 1);
            }
        }
    });

    button2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            if (panes.size() > 0) {
                // removing from both list and splitPane childs
                Pane toDelete = panes.remove(0);
                splitPane.getItems().remove(toDelete);
            }
        }
    });

此外,您可以直接从ScrollPane.getChildren()删除窗格,但它可能涉及棘手且不可靠的代码,以确定要删除的窗格。