gpt4 book ai didi

java - 使用选项卡的 onCloseRequest 事件会完全禁用选项卡

转载 作者:行者123 更新时间:2023-12-02 00:13:46 25 4
gpt4 key购买 nike

我正在为 JavaFX 应用程序编写一个应用程序内网络浏览器,该应用程序与所有浏览器一样具有选项卡。整个应用程序的结构基于 TabPane,其中显示了应用程序的所有功能。当用户选择使用浏览器时,将创建一个新的 Tab(我们称它为 browserMainTab),它包含另一个用于浏览器选项卡的 TabPane。当用户选择关闭 browserMainTab 但内部 TabPane 仍然打开选项卡时,我想显示一个确认对话框。到目前为止,一切正常,但是当我使用 browserMainTab 的 onCloseRequest 事件时,该选项卡及其所有子项仍然处于 Activity 状态,但无法单击、拖动或关闭。

private void mainBrowserTabCloseConfirmationEvent(Tab tab){

tab.setOnCloseRequest(event -> {
AnchorPane pane = (AnchorPane) tab.getContent();
TabPane browserSubTabPane = (TabPane) pane.getChildren().get(0);

if(browserSubTabPane.getTabs().size() >= 1){
StillOpenBrowserTabsAlert alert = new StillOpenBrowserTabsAlert();
alert.setNumOfOpenTabs(browserSubTabPane.getTabs().size());
Alert alert1 = alert.alertWithReturnType();

if(alert1.getResult() == ButtonType.OK){
getMainTabPane().getTabs().remove(getMainTabPane()
.getSelectionModel()
.getSelectedItem());
}else{
event.consume();
}
}
});
}

上面的代码片段没有抛出异常,第一部分工作正常。在我 consume() 事件之后,当标签变得不可点击时,问题出现了。有人可以帮忙吗?

         **EDIT**

可根据要求重现示例

ExampleMain.java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ExampleMain extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainTabPane.fxml"));
Parent root = loader.load();
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
}

MainTabPane.java

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainTabPane implements Initializable {
@FXML private TabPane mainTabPane;
private SubTabPane subTabPaneController;


private SubTabPane getSubTabPaneController() {
return subTabPaneController;
}

private void setSubTabPaneController(SubTabPane subTabPaneController) {
this.subTabPaneController = subTabPaneController;
}

private TabPane getMainTabPane() { return mainTabPane; }



private void loadSubTabPaneAsTabContent(){
FXMLLoader loader = new FXMLLoader(getClass().getResource("subTabPane.fxml"));
Parent root = null;
try{
root = loader.load();
}catch (IOException exception){
exception.printStackTrace();
}
assert root != null;
setSubTabPaneController(loader.getController());
Tab tab = new Tab("Tab of main TabPane");
tab.setContent(root);
getMainTabPane().getTabs().add(tab);
closeRequestOfMainTabPane(tab);
}



private void closeRequestOfMainTabPane(Tab tab){
tab.setOnCloseRequest(e -> {
int numOfOpenTabsInSubTabPane = getSubTabPaneController().getSubTabPane().getTabs().size();
if(numOfOpenTabsInSubTabPane >= 1){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.showAndWait();

if(alert.getResult() == ButtonType.OK){
getMainTabPane().getTabs().remove(getMainTabPane()
.getSelectionModel()
.getSelectedItem());

}else{
e.consume();
}
}
});
}


@Override
public void initialize(URL location, ResourceBundle resources) {
Platform.runLater(this::loadSubTabPaneAsTabContent);
getMainTabPane().setTabDragPolicy(TabPane.TabDragPolicy.REORDER);
}
}

SubTabPane.java


package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TabPane;

public class SubTabPane {
@FXML private TabPane subTabPane;

TabPane getSubTabPane() { return subTabPane; }

}

mainTabPane.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainTabPane">
<children>
<TabPane fx:id="mainTabPane" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" />
</children>
</AnchorPane>

subTabPane.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SubTabPane">
<children>
<TabPane fx:id="subTabPane" layoutX="7.0" prefHeight="400.0" prefWidth="593.0">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>

最佳答案

我在一个应用程序中尝试了类似的操作,其中的选项卡对应于打开的项目。当 TabDragPolicy 设置为 REORDER 时,问题出现了,我按下了一个选项卡关闭按钮,出现了一个确认对话框,询问我是否要保存项目,然后如果我取消标签关闭,事件就会被消耗掉。如果删除这三者中的任何一个(拖动策略、对话框或事件消耗),选项卡不会卡住。移除选项卡并将其添加回相同位置会有所帮助,但看起来很难看。

出于某种原因,以下方法非常有效:

tab.setOnCloseRequest(e -> {
tab.getTabPane().setTabDragPolicy(TabPane.TabDragPolicy.FIXED);

// your confirmation code

Platform.runLater(() -> tab.getTabPane().setTabDragPolicy(TabPane.TabDragPolicy.REORDER));
});

关于java - 使用选项卡的 onCloseRequest 事件会完全禁用选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com