🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #1372

Re: Dialog box association

#1372
From: 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)
Technical Details
Message-ID: <3fdf4e6b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!zeus.visi.com!green.octanews.net!blue.octanews.net!news.octanews.net!news-out.visi.com!petbe.visi.com!in.100proofnews.com!in.100proofnews.com!news02.tsnz.net!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <3fde2463@newsgroups.borland.com> <3fde265c$1@newsgroups.borland.com> <3fdf2537$1@newsgroups.borland.com> <3fdf40cd$1@newsgroups.borland.com> <3fdf4a1a$1@newsgroups.borland.com> <3fdf4cd0$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,55c92298dba121d1
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2003-12-16 10:40:55 PST
From: "Gillmer J. Derge \(TeamB\)" <spam@gillmerderge.com>
Newsgroups: borland.public.jbuilder.java.swing
References: <3fde2463@newsgroups.borland.com> <3fde265c$1@newsgroups.borland.com> <3fdf2537$1@newsgroups.borland.com> <3fdf40cd$1@newsgroups.borland.com> <3fdf4a1a$1@newsgroups.borland.com> <3fdf4cd0$1@newsgroups.borland.com>
Subject: Re: Dialog box association
Date: Tue, 16 Dec 2003 13:26:55 -0500
Lines: 38
MIME-Version: 1.0
Content-Type: text/plain;
 charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 64.185.133.49
Message-ID: <3fdf4e6b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1071599211 64.185.133.49 (16 Dec 2003 10:26:51 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!zeus.visi.com!green.octanews.net!blue.octanews.net!news.octanews.net!news-out.visi.com!petbe.visi.com!in.100proofnews.com!in.100proofnews.com!news02.tsnz.net!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail