Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab consuming in TabPane on default closing

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();
            }
        });
like image 563
Ronak Avatar asked Nov 24 '12 04:11

Ronak


4 Answers

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)
like image 182
jewelsea Avatar answered Nov 13 '22 12:11

jewelsea


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
         }
});
like image 4
Ronak Avatar answered Nov 13 '22 13:11

Ronak


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);
                    }
                }
            });
        }
    }
}
like image 1
ytw Avatar answered Nov 13 '22 12:11

ytw


Change to onMousePressed instead of setOnMouseReleased,

And also the close triggers to onMousePressed instead of OnMouseReleased.

like image 1
Nosfert Avatar answered Nov 13 '22 12:11

Nosfert