这是一个潜在的解决方案。如果您将自己的“提示用户”逻辑添加到下面的代码中,它应该可以满足您的需求。
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);
}
}
});
}
}
}