LucDaher
LucDaher

Reputation: 157

Identify whether JDialog is already opened

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)?

Upvotes: 3

Views: 4549

Answers (3)

user592704
user592704

Reputation: 3704

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

Upvotes: 1

user592704
user592704

Reputation: 3704

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

Upvotes: 2

spot35
spot35

Reputation: 888

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

Upvotes: 2

Related Questions