🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
13 messages
13 total messages Started by Matthew Sweet Mon, 15 Dec 2003 21:13
Dialog box association
#1497
Author: Matthew Sweet
Date: Mon, 15 Dec 2003 21:13
5 lines
1446 bytes
I've created a dialog box within a class that is instantiated by the main
Frame class. I run my program and the dialog box pops up, however when I
click off my application and then click back to it, the dialog box doesn't
come back with my main application. How to I associate my dialog box with
the main frame when the dialog box is created by another class?
Re: Dialog box association
#1498
Author: Lori M Olson (TeamB)
Date: Mon, 15 Dec 2003 21:26
31 lines
2178 bytes
Matthew Sweet wrote:

> I've created a dialog box within a class that is instantiated by the main
> Frame class. I run my program and the dialog box pops up, however when I
> click off my application and then click back to it, the dialog box doesn't
> come back with my main application. How to I associate my dialog box with
> the main frame when the dialog box is created by another class?
> 
> 

Did you give the dialog box an "owner" frame?  It needs to be "owned" by 
your main application window, in order to get the behavior you want.

-- 

Regards,

Lori Olson (TeamB)

------------

Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.

Google Advanced Newsgroup Search
     http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
     http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
     http://www.visi.com/~gyles19/fom-serve/cache/1.html
Re: Dialog box association
#1508
Author: Matthew Sweet
Date: Tue, 16 Dec 2003 15:29
47 lines
2855 bytes
That's the problem I'm having. Where I'm creating the dialog box I don't
know how to get to the Frame class to pass it in.

I'm basically trying to create my own color chooser. Something simple with
about 8 colors. I have my main frame that instantiates a class extending
JPanel which in turn instantiates another class extending JPanel and from
there instantiates the JDialog. I can't get to the Frame component from that
last JPanel class to pass it into the JDialog class I created.


"Lori M Olson (TeamB)" <javadragon@techie.com> wrote in message
news:3fde265c$1@newsgroups.borland.com...
> Matthew Sweet wrote:
>
> > I've created a dialog box within a class that is instantiated by the
main
> > Frame class. I run my program and the dialog box pops up, however when I
> > click off my application and then click back to it, the dialog box
doesn't
> > come back with my main application. How to I associate my dialog box
with
> > the main frame when the dialog box is created by another class?
> >
> >
>
> Did you give the dialog box an "owner" frame?  It needs to be "owned" by
> your main application window, in order to get the behavior you want.
>
> -- 
>
> Regards,
>
> Lori Olson (TeamB)
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
>      http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
>      http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
>      http://www.visi.com/~gyles19/fom-serve/cache/1.html
>
Re: Dialog box association
#1509
Author: Lori M Olson (TeamB)
Date: Tue, 16 Dec 2003 16:37
41 lines
2662 bytes
Matthew Sweet wrote:

> That's the problem I'm having. Where I'm creating the dialog box I don't
> know how to get to the Frame class to pass it in.
> 
> I'm basically trying to create my own color chooser. Something simple with
> about 8 colors. I have my main frame that instantiates a class extending
> JPanel which in turn instantiates another class extending JPanel and from
> there instantiates the JDialog. I can't get to the Frame component from that
> last JPanel class to pass it into the JDialog class I created.
> 

There's a couple of ways to do this.

If you think this is going to be a common problem, and you need to have 
a way to access that Frame in many places... Then make a static public 
method that returns a reference to your frame in your application class 
- MyApplication.getFrame()

Or, if this is an isolated instance, then you can "walk" up the parent 
list of your panels, until you get to a parent that is instance of a 
JFrame, and use it.

-- 

Regards,

Lori Olson (TeamB)

------------

Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.

Google Advanced Newsgroup Search
     http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
     http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
     http://www.visi.com/~gyles19/fom-serve/cache/1.html
Re: Dialog box association
#1367
Author: Matthew Sweet
Date: Tue, 16 Dec 2003 17:14
50 lines
3011 bytes
I've tried that but everything I try to pass out of that getFrame function
says it can't be referenced by static context.

"Lori M Olson (TeamB)" <javadragon@techie.com> wrote in message
news:3fdf342e@newsgroups.borland.com...
> Matthew Sweet wrote:
>
> > That's the problem I'm having. Where I'm creating the dialog box I don't
> > know how to get to the Frame class to pass it in.
> >
> > I'm basically trying to create my own color chooser. Something simple
with
> > about 8 colors. I have my main frame that instantiates a class extending
> > JPanel which in turn instantiates another class extending JPanel and
from
> > there instantiates the JDialog. I can't get to the Frame component from
that
> > last JPanel class to pass it into the JDialog class I created.
> >
>
> There's a couple of ways to do this.
>
> If you think this is going to be a common problem, and you need to have
> a way to access that Frame in many places... Then make a static public
> method that returns a reference to your frame in your application class
> - MyApplication.getFrame()
>
> Or, if this is an isolated instance, then you can "walk" up the parent
> list of your panels, until you get to a parent that is instance of a
> JFrame, and use it.
>
> -- 
>
> Regards,
>
> Lori Olson (TeamB)
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
>      http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
>      http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
>      http://www.visi.com/~gyles19/fom-serve/cache/1.html
>
Re: Dialog box association
#1368
Author: Gillmer J. Derge (TeamB)
Date: Tue, 16 Dec 2003 17:28
13 lines
1556 bytes
"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fdf2537$1@newsgroups.borland.com...
> I can't get to the Frame component from that
> last JPanel class to pass it into the JDialog class I created.

(JFrame)SwingUtilities.windowForComponent(panel);

Since windowForComponent returns a Window, you might want to verify that
it's an instance of JFrame before casting it, but this shows the basic
idea.

-- 
Gillmer J. Derge (TeamB)
Re: Dialog box association
#1369
Author: Matthew Sweet
Date: Tue, 16 Dec 2003 18:06
24 lines
2290 bytes
I get the same result with what you suggested. Before I had
(Frame)SwingUtilities.getAncestorOfClass(Frame.class, this) I guess they are
doing the same thing. Ultimately my problem is that I lose my dialog box in
the background when I click on another window and back onto my application
window. Is this a problem with an incorrect association or do I need to call
some other function to make it come back?


"Gillmer J. Derge (TeamB)" <spam@gillmerderge.com> wrote in message
news:3fdf40cd$1@newsgroups.borland.com...
> "Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
> news:3fdf2537$1@newsgroups.borland.com...
> > I can't get to the Frame component from that
> > last JPanel class to pass it into the JDialog class I created.
>
> (JFrame)SwingUtilities.windowForComponent(panel);
>
> Since windowForComponent returns a Window, you might want to verify that
> it's an instance of JFrame before casting it, but this shows the basic
> idea.
>
> -- 
> Gillmer J. Derge (TeamB)
>
Re: Dialog box association
#1371
Author: Matthew Sweet
Date: Tue, 16 Dec 2003 18:18
54 lines
3362 bytes
I think I figured it out. I had "ColorChooser colorChooserDlg = new
ColorChooser( (Frame) SwingUtilities.windowForComponent(this));" in the
initialization section of my containor class. I moved it to the mouse
adapter class. Dont' really understand why unless I needed to call
colorChooserDlg.show() somewhere else.


        // "this" is a JPanel. The code is basically from the About box code
        ColorChooser colorChooserDlg = new ColorChooser( (Frame)
SwingUtilities.windowForComponent(this));
        Dimension dlgSize = colorChooserDlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        colorChooserDlg.setLocation( (frmSize.width - dlgSize.width) / 2 +
loc.x,
                                    (frmSize.height - dlgSize.height) / 2 +
loc.y);
        colorChooserDlg.setModal(true);
        colorChooserDlg.pack();
        colorChooserDlg.show();



"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fdf4a1a$1@newsgroups.borland.com...
> I get the same result with what you suggested. Before I had
> (Frame)SwingUtilities.getAncestorOfClass(Frame.class, this) I guess they
are
> doing the same thing. Ultimately my problem is that I lose my dialog box
in
> the background when I click on another window and back onto my application
> window. Is this a problem with an incorrect association or do I need to
call
> some other function to make it come back?
>
>
> "Gillmer J. Derge (TeamB)" <spam@gillmerderge.com> wrote in message
> news:3fdf40cd$1@newsgroups.borland.com...
> > "Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
> > news:3fdf2537$1@newsgroups.borland.com...
> > > I can't get to the Frame component from that
> > > last JPanel class to pass it into the JDialog class I created.
> >
> > (JFrame)SwingUtilities.windowForComponent(panel);
> >
> > Since windowForComponent returns a Window, you might want to verify that
> > it's an instance of JFrame before casting it, but this shows the basic
> > idea.
> >
> > -- 
> > Gillmer J. Derge (TeamB)
> >
>
>
Re: Dialog box association
#1370
Author: Gillmer J. Derge (TeamB)
Date: Tue, 16 Dec 2003 18:21
10 lines
1557 bytes
"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fdf4a1a$1@newsgroups.borland.com...
> Ultimately my problem is that I lose my dialog box in
> the background when I click on another window and back onto my
application

Is the dialog modal?  It probably should be for what you're describing.

-- 
Gillmer J. Derge (TeamB)
Re: Dialog box association
#1372
Author: Gillmer J. Derge (TeamB)
Date: Tue, 16 Dec 2003 18:26
37 lines
2962 bytes
"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fdf4cd0$1@newsgroups.borland.com...
> I think I figured it out. I had "ColorChooser colorChooserDlg = new
> ColorChooser( (Frame) SwingUtilities.windowForComponent(this));" in
the
> initialization section of my containor class. I moved it to the mouse
> adapter class. Dont' really understand why unless I needed to call
> colorChooserDlg.show() somewhere else.

Check what you get back from windowForComponent in each case.  I suspect
that in the original code you were getting a null return value.

That method walks up the component tree from parent to parent until it
finds one that's a Window.  If your panel hasn't yet been added to its
parent, then it won't be able to find the frame.  In other words,
suppose you had something like this:

    JPanel myPanel = new MyPanel();
    // dialog created and windowForComponent called in the constructor
    frame.getContentPane().add(myPanel);

Then at the time windowForComponent is called, myPanel doesn't have a
parent.  It doesn't get a parent until it's added to the frame's content
pane.  So when windowForComponent searches for an ancestor that's a
Window, it can't find one, because there aren't any ancestors at all,
let alone a Window.

By putting it in the even handler, you've changed the sequence to
something like this:

    JPanel myPanel = new MyPanel();
    frame.getContentPane().add(myPanel);
    // ...
    // dialog created and windowForComponent called in an event handler

-- 
Gillmer J. Derge (TeamB)
Re: Dialog box association
#1377
Author: Matthew Sweet
Date: Wed, 17 Dec 2003 15:37
20 lines
2026 bytes
Yes it is modal.

I have another issue where I am unable to set the size of the dialog.
Whenever my dialog pops up it's basically just a title bar and the corner
exit button. I'm able to resize it and see my content, but it's initially
just the title bar.

"Gillmer J. Derge (TeamB)" <spam@gillmerderge.com> wrote in message
news:3fdf4d2b@newsgroups.borland.com...
> "Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
> news:3fdf4a1a$1@newsgroups.borland.com...
> > Ultimately my problem is that I lose my dialog box in
> > the background when I click on another window and back onto my
> application
>
> Is the dialog modal?  It probably should be for what you're describing.
>
> -- 
> Gillmer J. Derge (TeamB)
>
Re: Dialog box association
#1379
Author: Matthew Sweet
Date: Wed, 17 Dec 2003 20:24
26 lines
2129 bytes
I figured out the size issue. I had to remove JDialog.pack();

"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fe078ac$1@newsgroups.borland.com...
> Yes it is modal.
>
> I have another issue where I am unable to set the size of the dialog.
> Whenever my dialog pops up it's basically just a title bar and the corner
> exit button. I'm able to resize it and see my content, but it's initially
> just the title bar.
>
> "Gillmer J. Derge (TeamB)" <spam@gillmerderge.com> wrote in message
> news:3fdf4d2b@newsgroups.borland.com...
> > "Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
> > news:3fdf4a1a$1@newsgroups.borland.com...
> > > Ultimately my problem is that I lose my dialog box in
> > > the background when I click on another window and back onto my
> > application
> >
> > Is the dialog modal?  It probably should be for what you're describing.
> >
> > -- 
> > Gillmer J. Derge (TeamB)
> >
>
>
Re: Dialog box association
#1380
Author: Gillmer J. Derge (TeamB)
Date: Wed, 17 Dec 2003 20:30
14 lines
1770 bytes
"Matthew Sweet" <matthew.sweet@sed.redstone.army.mil> wrote in message
news:3fe0bbd5$1@newsgroups.borland.com...
> I figured out the size issue. I had to remove JDialog.pack();

Then it sounds like you might have a problem with your layout manager.
The dialog ought to be able to figure out its preferred size so pack
works properly.

That is, unless you're using something like the XYLayout in which case
you definitely don't want to use pack, but you might want to consider
switching layout managers.

-- 
Gillmer J. Derge (TeamB)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads