Article View: borland.public.jbuilder.java.swing
Article #10280Re: Math for centering dialog box inside a main frame
From: Paul Furbacher [TeamB]
Date: Sun, 24 Oct 1999 00:00
Date: Sun, 24 Oct 1999 00:00
40 lines
1882 bytes
1882 bytes
Jeff Marendo wrote:
> I have tried to use what appears to be somewhat of a standard math equation
> for always centering a dialog window (in my case, an 'About' box from the
> Help menu), but the window doesn't appear centered when executed. It is
> also misaligned if I resize the main window.
>
> Ideally, I'd like to have the 'About' dialog always appear within the
> confines of the window in which it was opened from (and have it be
> completely visible). I tried basing my algorithm off of the main frame's
> width and height (and the dialog's): it almost centers it, but this is
> pretty much hardcoding it into one spot. I know I have to use the sizes.
Well, if I were to set about doing this, I'd try the following:
0. Get the parent window of the dialog box.
Window parent = this.getParent();
1. Get the location of the parent window.
Point parentLoc = parent.getLocation();
2. Get the width and height of the parent window.
int parentWidth = parent.getWidth();
int parentHeight = parent. getHeight();
3. Do the math:
int thisX = parentLoc.x + (parentWidth - this.getWidth())/2;
int thisY = parentLoc.y + (parentHeight - this.getHeight())/2;
That should do it.
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<38134D49.4F854363@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7upraq$npj6@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,869a3aa575acdb31 X-Google-Attributes: gid111d45,public From: "Paul Furbacher [TeamB]" <pf@bilbo.bio.purdue.edu> Subject: Re: Math for centering dialog box inside a main frame Date: 1999/10/24 Message-ID: <38134D49.4F854363@bilbo.bio.purdue.edu> X-Deja-AN: 540012429 Content-Transfer-Encoding: 7bit References: <7upraq$npj6@forums.borland.com> Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pf@bilbo.bio.purdue.edu Newsgroups: borland.public.jbuilder.java.swing