- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 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/
我正在为 JavaFX 应用程序编写一个应用程序内网络浏览器,该应用程序与所有浏览器一样具有选项卡。整个应用程序的结构基于 TabPane,其中显示了应用程序的所有功能。当用户选择使用浏览器时,将创建
我正在为 JavaFX 应用程序编写一个应用程序内网络浏览器,该应用程序与所有浏览器一样具有选项卡。整个应用程序的结构基于 TabPane,其中显示了应用程序的所有功能。当用户选择使用浏览器时,将创建
在我的程序的 GUI 中,我有一行: window.setOnCloseRequest(closeProgram()); 只要用户按下窗口框架上的 X 按钮(不是我制作的按钮,而是每个 GUI 应用程
tab.setOnCloseRequest(e -> { if (getEditorForTextArea(getSelectedTextArea()) != null
请参阅下面的 JavaFX SSCCE。当从窗口的默认标题栏“X”按钮关闭主阶段时,会出现打印语句。单击“关闭”按钮时不会出现打印语句。当我在舞台上调用 close() 时,为什么我的 onClose
我是一名优秀的程序员,十分优秀!