Tab consuming in TabPane on default closing

2019-04-07 01:41发布

I have created a program in which a TabPane contains multiple Tabs. When someone tries to close any Tab, I want to execute my own code: when a user clicks on the default close button for any Tab, it will ask for confirmation. If the user says "Yes" then the tab will be closed, otherwise it will remain open.

How can I do this?

I am doing something like below. but the Tab is still getting closed. How would I consume that 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();
            }
        });

4条回答
聊天终结者
2楼-- · 2019-04-07 02:16

The Tab implementation for Java 8 has an onCloseRequest property which allows you to prevent tab closing:

/* The installed event handler can prevent tab closing 
   by consuming the received event. */
public void setOnCloseRequest(EventHandler<Event> value)
查看更多
仙女界的扛把子
3楼-- · 2019-04-07 02:16

Here's a potential solution. If you add your own "prompt 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);
                    }
                }
            });
        }
    }
}
查看更多
爷的心禁止访问
4楼-- · 2019-04-07 02:18

I am getting my own way as given below.
I have created a hyperlink and set it as graphic for that Tab and its work fine 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
         }
});
查看更多
虎瘦雄心在
5楼-- · 2019-04-07 02:19

Change to onMousePressed instead of setOnMouseReleased,

And also the close triggers to onMousePressed instead of OnMouseReleased.

查看更多
登录 后发表回答