Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove close button from tabs in JavaFX

Tags:

java

javafx

I have created some tabs in a TabPane. Each time I make a tab it has got a close(x) button on its right side. I don't want the tabs to be removed from the TtabPane so I have used:

 TabPane tabPane = new TabPane();
 Tab tab = new Tab("new tab");
 tab.setContents(new Label("Please help"));
 tabPane.getTabs().add(tab);
 tab.setOnCloseRequest(e -> e.consume());

so that it won't be removed. Is there some way not to display this close button on tab.

Any help is appreciated.

like image 593
persistent_poltergeist Avatar asked Jul 21 '15 05:07

persistent_poltergeist


2 Answers

You can set the TabClosingPolicy on a TabPane

myTabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);

There are the following possibilities:

  • TabClosingPolicy.ALL_TABS // all tabs can be closed
  • TabClosingPolicy.SELECTED_TAB // only the selected tab can be closed
  • TabClosingPolicy.UNAVAILABLE // you cant close

If you are adding classes to myTabPane.getTabs() there is also the possibility to set the class to not be closeable (because it needs to extend from Tab):

setClosable(false);

If you define it in the class which extends from Tab I guess the policy you set will be useless and is overridden.

Link to the oracle doc: JavaFX 8 TabPane.TabClosingPolicy

like image 145
NDY Avatar answered Nov 04 '22 05:11

NDY


You can also define this using FXML by this code:

<TabPane tabClosingPolicy="UNAVAILABLE">
like image 4
Pochmurnik Avatar answered Nov 04 '22 07:11

Pochmurnik