This discussion is archived
2 Replies Latest reply: Nov 19, 2012 7:29 AM by 974949 RSS

JavaFX closable tabs

974949 Newbie
Currently Being Moderated
Hello all,

I've been trying to create a tabpane with closeable tabs, but the close buttons don't appear. I used scene builder, putting in a scene a tabpane and two tabs, settin closeable to true in each one, to no avail.

Also, I've tried writing it by code:


Tab tab = new Tab(titulo);
final ObservableList<Tab> tabs = getTabPane().getTabs();
tab.setContent(myControl);
tab.setClosable(true);
tabs.add(tab);

With this code, the close button does not appear either. What am I doing wrong?

Thanks for your help
  • 1. Re: JavaFX closable tabs
    dscarminiabielefeld Newbie
    Currently Being Moderated
    Hey,
    they should be closeable by default.
    Have you implemented a TabPane and added the Tabs into the Pane?

    This should work, look at the code and compare to yours. maybe you forgot something small.
    package yourpackage;
    
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class AddingTabs extends Application 
    {
        public static void main(String[] args) 
        {
            Application.launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) 
        {
            Group root = new Group();
            Scene scene = new Scene(root, 400, 250, Color.WHITE);
    
            TabPane tabPane = new TabPane();
    
            BorderPane borderPane = new BorderPane();
    
            // generate 10 tabs
            for (int i = 0; i < 10; i++) 
            {
                Tab tab = new Tab();
                tab.setText("Tab" + i);
                HBox hbox = new HBox();
                hbox.getChildren().add(new Label("Tab" + i));
                hbox.setAlignment(Pos.CENTER);
                tab.setContent(hbox);
                tabPane.getTabs().add(tab);
            }
            
            // add tab pane
            borderPane.setCenter(tabPane);
    
            // bind to take available space
            borderPane.prefHeightProperty().bind(scene.heightProperty());
            borderPane.prefWidthProperty().bind(scene.widthProperty());
    
            // add border Pane
            root.getChildren().add(borderPane);
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
  • 2. Re: JavaFX closable tabs
    974949 Newbie
    Currently Being Moderated
    Hello,

    I've tried your code and, exactly, it works. I reviewed my code and realized the only thing that changed was that I loaded the control containing the tabpane from a scene builder generated pane. So I checked the properties in the controls and found that, by default, TabPane has TabClosingPolicy to UNAVAILABLE, so it wasn't possible to close the tabs.

    Thank you very much :)

Legend

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