This discussion is archived
3 Replies Latest reply: Aug 20, 2013 9:12 AM by acepsut RSS

SplitPane: how to add an x icon to remove a pane?

acepsut Newbie
Currently Being Moderated

I would like to have an x closable icon on every panes of a SplitPane in order to remove a pane, such as this picture

 

http://s9.postimg.org/uhn7vgr6n/Z096.png

 

The x icon is at the rightmost side just after the MACD(12, 26, 9) label

 

Is this possibile? And if so, how to? An example on my code below is really appreciated

 

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
/**
*
* @author utente
*/
public class ClosableSplitPane extends Application {
   
    @Override
    public void start(Stage primaryStage) {
              
        Pane root = new Pane();
        SplitPane sp = new SplitPane();
        sp.setPrefSize(800, 650);
       
        Pane paneOne = new Pane();
        Pane paneTwo = new Pane();
        Pane paneThree = new Pane();
        Pane paneFour = new Pane();
       
        Label labelFirst = new Label(" Pane One");
        Label labelSecond = new Label(" Pane Two");
        Label labelThird = new Label(" Pane Three");
       
        paneTwo.getChildren().add(labelFirst);
        paneThree.getChildren().add(labelSecond);
        paneFour.getChildren().add(labelThird);
       
        sp.setDividerPositions(0.5f, 0.65f, 0.8f);
       
        sp.getItems().addAll(paneOne, paneTwo, paneThree, paneFour);
        sp.setVisible(true);
        sp.setOrientation(Orientation.VERTICAL);
       
        root.getChildren().add(sp);
       
        Scene scene = new Scene(root, 800, 650);
       
        primaryStage.setTitle("SplitPane dragging test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
  
    public static void main(String[] args) {
        launch(args);
    }  
}

  • 1. Re: SplitPane: how to add an x icon to remove a pane?
    James_D Guru
    Currently Being Moderated

    Add a button or icon to the pane and register the appropriate event handler. In the event handler, just call

    sp.getItems().remove(thePane);
  • 2. Re: SplitPane: how to add an x icon to remove a pane?
    shakir.gusaroff Expert
    Currently Being Moderated

    It is possible.  In this case the divider positions doesn’t make sense.

     

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.geometry.Orientation;
    import javafx.scene.Group;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.SplitPane;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.CircleBuilder;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    public class ClosableSplitPane extends Application {
        @Override
        public void start(Stage primaryStage) {
            Pane root = new Pane();
            final SplitPane sp = new SplitPane();
            sp.setPrefSize(800, 650);
            final Pane paneOne = new Pane();
            final Pane paneTwo = new Pane();
            final Pane paneThree = new Pane();
            final Pane paneFour = new Pane();
            CustomExit ceOne = new CustomExit();
            ceOne.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    sp.getItems().remove(paneOne);
                }
            });
            paneOne.getChildren().addAll(ceOne);
            ceOne.translateXProperty().bind(paneOne.widthProperty().subtract(15));
            ceOne.setTranslateY(10);
            CustomExit ceTwo = new CustomExit();
            ceTwo.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    sp.getItems().remove(paneTwo);
                }
            });
            paneTwo.getChildren().addAll(ceTwo);
            ceTwo.translateXProperty().bind(paneTwo.widthProperty().subtract(15));
            ceTwo.setTranslateY(10);
            CustomExit ceThree = new CustomExit();
            ceThree.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    sp.getItems().remove(paneThree);
                }
            });
            paneThree.getChildren().addAll(ceThree);
            ceThree.translateXProperty().bind(paneThree.widthProperty().subtract(15));
            ceThree.setTranslateY(10);
            CustomExit ceFour = new CustomExit();
            ceFour.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    sp.getItems().remove(paneFour);
                }
            });
            paneFour.getChildren().addAll(ceFour);
            ceFour.translateXProperty().bind(paneFour.widthProperty().subtract(15));
            ceFour.setTranslateY(10);
            Label labelFirst = new Label(" Pane One");
            Label labelSecond = new Label(" Pane Two");
            Label labelThird = new Label(" Pane Three");
            paneTwo.getChildren().add(labelFirst);
            paneThree.getChildren().add(labelSecond);
            paneFour.getChildren().add(labelThird);
            sp.setDividerPositions(0.5f, 0.65f, 0.8f);
            sp.getItems().addAll(paneOne, paneTwo, paneThree, paneFour);
            sp.setVisible(true);
            sp.setOrientation(Orientation.VERTICAL);
            root.getChildren().add(sp);
            Scene scene = new Scene(root, 800, 650);
            primaryStage.setTitle("SplitPane dragging test");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
        class CustomExit extends Group {
            public CustomExit() {
                Circle closeButton = CircleBuilder.create()
                        .centerX(5)
                        .centerY(0)
                        .radius(7)
                        .fill(Color.rgb(100, 100, 100, .80))
                        .build();
                Text closeText = new Text(2, 4, "X");
                getChildren().addAll(closeButton, closeText);
            }
        }
    }
  • 3. Re: SplitPane: how to add an x icon to remove a pane?
    acepsut Newbie
    Currently Being Moderated

    Thank you very much!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points