如果主应用程序最小化,则从 JDialog 中移除焦点

发布于 2024-11-02 05:20:15 字数 1929 浏览 1 评论 0原文

我有一个 JDialog,每当我的主应用程序收到事件时就会弹出。 我面临的问题是,即使主窗口最小化,对话框也会弹出。这里提出了类似的问题,但没有给出如何解决此问题的答案,除了有关焦点处理的 sun 指南的链接 当窗口失去焦点时隐藏 JDialog 窗口
假设我有函数 createandshowDialog() ,例如

public void createAndShowDialog(boolean manualLaunch) {
        if (manualLaunch || shouldShowMessage()) {
            SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {                        
                        if (dialog == null) {
                            dialog = new xyzDialog(getPendingList());
                        }
                        GUIHelper.flash(PLAFHelper.getMainFrame(), true, false);
                        dialog.setVisible(true);
                   }
                });
            }
}

xyzDialog 类定义为:

  public class xyzDialog extends SimpleStandardDialog
  {
    protected final static Log logger = LogFactory.getLog(xyzDialog.class);

    private xyzPanel panel;

   public xyzDialog(ObjectArrayList list) {
        super(PLAFHelper.getMainFrame(), "Pending Cancel/Replace");
        initializeLocalVars();
        panel = new xyzPanel(list, mediator);
        super.setSize(750,600);
        setResizable(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setModal(false);//todo: for testing...

        if(PUMAGUIOptions.cFocusOnxyzPopup){
            setFocusableWindowState(true);
            validate();
        }
        else{
            setFocusableWindowState(false);
            validate();

        }

    }

默认行为应该是这样的,如果主窗口最小化,或者我们显式将 cFocusOnxyzPopup 设置为 false 以强制此默认行为(这是当它在辅助监视器上打开并且我们正在主监视器上工作或应用程序最大化或处于后台时的情况下,即不是 focusOwner。
我已将 focusableWindowState 设置为 false,这样它就不会满足获得焦点的条件,并且如果按照 java 文档中给出的方式调用,则将 isFocusable 返回为 false。但这是行不通的。有什么建议吗?

I have a JDialog which popup whenever my main application recieves an event.
The problem I'm facing is that the dialog pops up evenif the main window is minimised. A similar question was asked here but no answer was given as to how to solve this except a link to sun's guide on focus handling Hide JDialog window when the window lost focus.
Suppose I have the function createandshowDialog() such as

public void createAndShowDialog(boolean manualLaunch) {
        if (manualLaunch || shouldShowMessage()) {
            SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {                        
                        if (dialog == null) {
                            dialog = new xyzDialog(getPendingList());
                        }
                        GUIHelper.flash(PLAFHelper.getMainFrame(), true, false);
                        dialog.setVisible(true);
                   }
                });
            }
}

And the xyzDialog class is defined as :

  public class xyzDialog extends SimpleStandardDialog
  {
    protected final static Log logger = LogFactory.getLog(xyzDialog.class);

    private xyzPanel panel;

   public xyzDialog(ObjectArrayList list) {
        super(PLAFHelper.getMainFrame(), "Pending Cancel/Replace");
        initializeLocalVars();
        panel = new xyzPanel(list, mediator);
        super.setSize(750,600);
        setResizable(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setModal(false);//todo: for testing...

        if(PUMAGUIOptions.cFocusOnxyzPopup){
            setFocusableWindowState(true);
            validate();
        }
        else{
            setFocusableWindowState(false);
            validate();

        }

    }

The default behaviour should be such that it should not popup if main window is minimised or we explicitly set cFocusOnxyzPopup as false to force this default behaviour (which is the case when it is open on say secondary monitor and we are working on primary monitor or application is maximised or is in background i.e. is not the focusOwner.
I have set focusableWindowState as false so that it would not satisy the condition for gaining focus and return isFocusable as false if invoked as given in java-docs. But this is not working. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-11-09 05:20:15

如果是 JFrame.ICONIFIED,请使用 JFrame 的方法,

public synchronized int getExtendedState()

e.g. PLAFHelper.getMainFrame().getExtendedState()

跳过对话框打开。

USe JFrame's method

public synchronized int getExtendedState()

e.g. PLAFHelper.getMainFrame().getExtendedState()

if it's JFrame.ICONIFIED skip the dialog opening.

难理解 2024-11-09 05:20:15

我遇到了同样的问题并搜索了stackoverflow!最后,我找到了

Markus Lausberg 回答的正确答案
JDialog 让主应用程序失去焦点

只需在对话框创建期间调用以下方法:

setFocusableWindowState (错误的);
setFocusable(假);

I have meet the same problem and search the stackoverflow ! finally, I find out the

corrent answer answered by Markus Lausberg in
JDialog lets main application lose focus

just calling the following method during dialog create:

setFocusableWindowState(false);
setFocusable(false);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文