如何仅关闭JDialog并使JFrame仍然可用-已解决

问题描述 投票:0回答:1

我有一个JFrame,并添加了一个JPanel。单击面板中的一个按钮时,会显示一个JDialog(名为choiceDialog)。当我单击对话框上的特定按钮时,我只希望它关闭。

我希望对话框关闭并且框架可用。有可能吗?

我试图用setVisible(false)隐藏对话框,但它同时隐藏了对话框和框架。然后我尝试执行choiceDialog.dispose(),但我又失去了两个要素。到那时,我找到了一种将框架重新设置为可见但不可用的方法。

有人可以帮我吗?我真的不知道该怎么办。

以下是相关代码:

if (dimField.isEnabled()){
    String dimFieldText = dimField.getText();
    if (dimFieldText.equals("") || !isNumeric(dimFieldText)){ //if there's an error when filling the options in the JDialog             
        errorLabel = new JLabel(noDim, SwingConstants.CENTER);
        /*other stuff
             ...
         */
    }else{ //if it's all ok: I want the JDialog close but the JFrame to be usable                   
        JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); //to catch the JFrame istance
        choiceDialog.dispose();
        topFrame.setVisible(true); //to make the JFrame visible again
        //choiceDialog.setVisible(false);
    }
java swing jframe jpanel jdialog
1个回答
0
投票

您正在将topFrame设置为按钮本身的对话框的窗口祖先。您需要获取对话框的窗口祖先。假定您在创建对话框时将主框架指定为其父框架,而不使用无参数的JDialog构造函数。

© www.soinside.com 2019 - 2024. All rights reserved.