Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to check if a certain JFrame exists

2,750 views
Skip to first unread message

Daniel Moritz

unread,
Jul 20, 2003, 5:02:02 PM7/20/03
to
Hi!
Another one of those annoying Newbie-questions:

I got a JFrame, which is created by a method invoked by a Button.
Works fine.

Now I want to let the method check if the JFrame is already open,
because it's not necessary to open it again.

Any way to do this?
I searched java.sun.com and this newsgroup, bu to no avail.
Any help welcomed

Thomas Weidenfeller

unread,
Jul 21, 2003, 2:39:48 AM7/21/03
to
Daniel Moritz <dai...@web.de> writes:
> I got a JFrame, which is created by a method invoked by a Button.
> Works fine.
>
> Now I want to let the method check if the JFrame is already open,
> because it's not necessary to open it again.
>
> Any way to do this?
> I searched java.sun.com and this newsgroup, bu to no avail.

The place to look for such things is the API documentation.
Window.isShowing(), or Component.isVisible() (both inherited by JFrame)
might be what you want.

/Thomas

Daniel Moritz

unread,
Jul 21, 2003, 3:28:51 AM7/21/03
to
OK, thanks

Daniel Moritz

unread,
Jul 21, 2003, 7:51:45 AM7/21/03
to
Hmm, it won't work
here's some of my code, maybe it helps:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame = new cardDialog();
frame.setVisible(true); //necessary as of 1.3
meinDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException fe) {}
}
}
);
That's the code I use to add a new JFrame (cardDialog) to my
JDesktopPane. I don't get it where I should look for an existing
instance of my JFrame

Jacob

unread,
Jul 21, 2003, 8:00:55 AM7/21/03
to
Daniel Moritz wrote:
> Hmm, it won't work
> here's some of my code, maybe it helps:
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> frame = new cardDialog();
> frame.setVisible(true); //necessary as of 1.3
> meinDesktop.add(frame);
> try {
> frame.setSelected(true);
> } catch (java.beans.PropertyVetoException fe) {}
> }
> }
> );
> That's the code I use to add a new JFrame (cardDialog) to my
> JDesktopPane. I don't get it where I should look for an existing
> instance of my JFrame

Make your dialog a private member of the class
and null it in the constructor. Then in the
actionPerformed() method you do something
like this:

// Create on first access
if (dialog == null) {
dialg = new JFrame();
... other init stuff ...
}

// Show it
dialog.setVisible (true);

Daniel Moritz

unread,
Jul 21, 2003, 9:46:44 AM7/21/03
to
Hmm, okay, this works fine now, first time I click the button the JFrame
shows and all clicks afterwards are ignored.
But when I close the JFrame (by the little x in the top-right corner) it
won't come back again when I click the button

Jacob

unread,
Jul 21, 2003, 10:03:43 AM7/21/03
to
Daniel Moritz wrote:
> Hmm, okay, this works fine now, first time I click the button the JFrame
> shows and all clicks afterwards are ignored.
> But when I close the JFrame (by the little x in the top-right corner) it
> won't come back again when I click the button

This program does what you're looking for.
Check how yours is different:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test implements ActionListener
{
private JFrame dialog_ = null;

public void actionPerformed (ActionEvent event)
{
if (dialog_ == null)
dialog_ = new JFrame("TEST");
dialog_.setVisible (true);
}

public static void main (String args[])
{
JFrame f = new JFrame();
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JButton b = new JButton ("Dialog...");
b.addActionListener (new Test());

f.getContentPane().add (b);

f.pack();
f.setVisible (true);
}
}


Daniel Moritz

unread,
Jul 21, 2003, 10:22:26 AM7/21/03
to

Hmm, still doesn't work.
Does it matter that cardDialog is a JInternalFrame?
Maybe the JInternalFrame 's DefaultCloseOperation doesn't dispose of it
the way I suppose it would?
Should I alter the JInternalFrames DefaultCloseOperation?

Karol Kisielewski

unread,
Jul 22, 2003, 9:07:04 AM7/22/03
to

"Daniel Moritz" <dai...@web.de> wrote in message
news:bfgk42$mhb$00$1...@news.t-online.com...

> Hmm, it won't work
> here's some of my code, maybe it helps:
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> frame = new cardDialog();
> frame.setVisible(true); file://necessary as of 1.3

> meinDesktop.add(frame);
> try {
> frame.setSelected(true);
> } catch (java.beans.PropertyVetoException fe) {}
> }
> }
> );
> That's the code I use to add a new JFrame (cardDialog) to my
> JDesktopPane. I don't get it where I should look for an existing
> instance of my JFrame

JDesktopPane.getAllFrames()


0 new messages