Tab Tab in TabPane on Default Close

I created a program in which I created TabPane in which there are several Tabs . I want to do this when someone tries to close any Tab, then I want to execute my own code. when the user clicks the default close button for any tab, then confirmation is requested. If the user says "Yes", the tab will be closed, otherwise it will remain open.

How can i do this?

I am doing something like below. but still closing. How will I use this tab?

Tab tab = new Tab(); TabPane tabPane=new TabPane(); tabPane.getTabs().add(tab); tab.setOnClosed(new EventHandler<Event>() { @Override public void handle(Event t) { t.consume(); } }); 
+7
source share
4 answers

I get my own path as shown below.
I created a hyperlink and set it as the graphic for this tab, and its work is excellent for me.

 Hyperlink hlink = new Hyperlink(); Image image = new Image(MyClass.class.getResourceAsStream("/images/close.png")); hlink.setGraphic(new ImageView(image)); hlink.setFocusTraversable(false); Tab tab = new Tab(); tab.setGraphic(hlink); hlink.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { //Do somthing } }); 
+4
source

The Tab implementation for Java 8 has the onCloseRequest property, which helps prevent tabs from closing:

 /* The installed event handler can prevent tab closing by consuming the received event. */ public void setOnCloseRequest(EventHandler<Event> value) 
+6
source

Here is a potential solution. If you add your own β€œtell the user” logic to the code below, it should do what you want.

 package com.test; import java.util.Set; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class JavaFXApp extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("http://stackoverflow.com/questions/13538227/tab-consuming-in-tabpane-on-default-closing"); BorderPane rootPane = new BorderPane(); Scene scene = new Scene(rootPane, 640, 360, Color.WHITE); TabPane tabPane = new TabPane(); Tab tab1 = new Tab(); tab1.setText("Tab 1"); tabPane.getTabs().add(tab1); Tab tab2 = new Tab(); tab2.setText("Tab 2"); tabPane.getTabs().add(tab2); rootPane.setCenter(tabPane); rootPane.prefHeightProperty().bind(scene.heightProperty()); rootPane.prefWidthProperty().bind(scene.widthProperty()); primaryStage.setScene(scene); primaryStage.show(); Set<Node> nodes = tabPane.lookupAll(".tab-close-button"); for (final Node node : nodes) { node.setUserData(node.getOnMouseReleased()); node.setOnMouseReleased(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent arg0) { boolean removeTab = false; // prompt the user if (removeTab) { ((EventHandler<MouseEvent>) node.getUserData()).handle(arg0); } } }); } } } 
+1
source

Change to onMousePressed instead of setOnMouseReleased ,

As well as closing triggers up to onMousePressed instead of OnMouseReleased .

+1
source

All Articles