判断JDialog是否已经打开

发布于 2024-11-24 21:52:24 字数 492 浏览 1 评论 0原文

如何识别 JDialog 组件是否已打开,从而避免同一个 JDialog 在同一个应用程序实例中打开两次?

我想到的一个解决方案是验证一个对象是否已经是 JDialog 实例(dialogObj instanceof JDialog),如果是,我只需调用负责其构造/展示的方法,如果不是,我只需创建一个新的 JDialog 实例(填充如果我有妄想,请随时纠正我)。

假设我创建了一个 JDialog,其中包含一 (1) 个 JPanel、一 (1) 个 JTextField、一 (1) 个 JButton 以及保存每次将“显示” JDialog 的事件的元素是 JMenuItem -> JPopUpMenu-> TrayIcon(系统托盘图标)。

我几乎找到了解决它的方法(如第二段所示),但是,当我通过系统托盘再次打开它时,我在“处理”对话框之前输入的所有内容都会再次出现,更不用说其他元素状态保持不变(JButton 启用等 - 这是另一个故事)。

这里有人知道如何解决它吗(当然有)?

How do I identify whether a JDialog component is already opened or not, thus, it would avoid the same JDialog to be opened twice at the same application instance?

One solution I had in mind was to verify whether an object is already a JDialog instance (dialogObj instanceof JDialog), if so, I just call the method responsible for its construction / exhibition, if not, I just create a new JDialog instance (fill free to correct me if I'm delusional).

Let's suppose I've created a JDialog containing one (1) JPanel, one (1) JTextField, one (1) JButton and the element that holds the event who will "display" the JDialog every time is a JMenuItem -> JPopUpMenu -> TrayIcon (System Tray icon).

I've almost discovered a way to solve it (as shown at the second paragraph), however, when I open it again through the System Tray, everything what I've typed before I've "disposed" the dialog box appears again, not to mention the others elements status that remains the same (JButton enabled etc - its another story).

Does anyone here have a clue how to solve it (of course it does)?

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

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

发布评论

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

评论(3

匿名。 2024-12-01 21:52:24

要解决这两项任务,

  • A)仅初始化一个 JDialog
  • B)检查它是否打开

您可以简单地使用模态 JDialog 作为

JDialog aDialog=new JDialog();
aDialog.setModal(true);

...这不会让用户初始化另一个 JDialog 示例:)

祝你好运

To solve both tasks as

  • A) Init just ONE JDialog
  • B) To check is it opened

You can simply use a modal JDialog as

JDialog aDialog=new JDialog();
aDialog.setModal(true);

...this won't let user to init another JDialog example :)

Good luck

纸短情长 2024-12-01 21:52:24

如果您有权访问 JDialog 实例,则只需调用 isVisible() 即可。如果它显示出来,那就是真的。

If you have access to the JDialog instance, then you can simply call isVisible(). If it is showing, this will be true.

梦旅人picnic 2024-12-01 21:52:24

作为一个变体,这里只是另一个基本概念(未测试)...

public class MyDialog extends JDialog
{

private boolean isOpen;

public MyDialog()
{

  this.setOpenStatus(true) ;      

}

private void setOpenStatus(boolean isOpen)
{
 this.isOpen=isOpen;
}

public boolean isDialogOpen(){return this.isOpen;}

}

//在您的基本应用程序深处的某个地方...

public class aClass{

private MyDialog aDialog;//field



public void actionPerformed(ActionEvent e)
{

  if(e.getActionCommand().equals("DIALOG_OPEN"))
{
    if(this.aDialog.isDialogOpen())
     {
       System.out.println("Dialog is opened"); return;
     }

    if(!this.aDialog.isDialogOpen())
    {
      this.aDialog=new MyDialog();
      this.aDialog.addWindowListener(...);
      //set JDialog options...
    }
}


}

}//end aClass

所以它也可以使用

祝你好运

As a variation here is just another basis conception (not tested)...

public class MyDialog extends JDialog
{

private boolean isOpen;

public MyDialog()
{

  this.setOpenStatus(true) ;      

}

private void setOpenStatus(boolean isOpen)
{
 this.isOpen=isOpen;
}

public boolean isDialogOpen(){return this.isOpen;}

}

//somewhere in your base app deeps...

public class aClass{

private MyDialog aDialog;//field



public void actionPerformed(ActionEvent e)
{

  if(e.getActionCommand().equals("DIALOG_OPEN"))
{
    if(this.aDialog.isDialogOpen())
     {
       System.out.println("Dialog is opened"); return;
     }

    if(!this.aDialog.isDialogOpen())
    {
      this.aDialog=new MyDialog();
      this.aDialog.addWindowListener(...);
      //set JDialog options...
    }
}


}

}//end aClass

So it can be used as well

Good luck

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