1

I can add SplitPane in a XYChart by clicking the "Add Pane" button, and it works fine.

Now I would like to remove SplitPane by clicking the "Remove pane" button.

Here is full code

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);
        }
      }
    });
}
}

Any help really appreciated.

4

1 回答 1

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()但它可能涉及棘手且不可靠的代码来确定要删除的窗格。

于 2012-07-17T10:38:30.103 回答