setLocation in Dialog

setLocation( x, y ) is suppose to move the component x
pixels right and y pixels down from the top-left corner of
its parent component.  But I have not been able to get this
behaviour in JDK1.1.x.  Does anyone have experience with
setLocation and could tell me if it is something I did wrong
or it is not implemented correctly in JDK.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What is your code what happens when you run it? What you've described is the behavior to expect. You may want to call invalidate() and/or validate() to make sure the screen is updated.
omokAuthor Commented:
int x = (frame.getSize().width - dialog.getSize().width) / 2;
int y = (frame.getSize().height - dialog.getSize().height) / 2;
dialog.setLocation(x, y);

The above code is an attempt to center the dialog, where the frame is the parent of the dialog.  But rather than centering the dialog inside the frame.  The dialog box is somewhere near the top-left corner of my screen...
The documentation is confusing.

The frame is not considered the dialog's parent for this purpose.

You are positioning it relative to the screen.
Add the x,y of the frame.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

I also experience relative to screen positioning when use this method.
When the documentation says its relative to the 'parent', it means relative
to the container the component was added to.
In the case of a dialog, the 'parent' does NOT contain the dialog. The term 'parent'
here is with regard to modality.

omokAuthor Commented:
>The term 'parent' here is with regard to modality.
But I passed the frame as parent in the constructor of the dialog, and my dialog was set to modal...
Like so: new Dialog(frame, true);

OK, what I was explaining is the confusion in the documentation. The frame
is the dialog's 'parent' with regard to modality (you did OK above), BUT the frame
is NOT the dialog's CONTAINER parent.
Now, setLocation documentation refers to the CONTAINER parent.
The dialog is not contained in another window and therefore the coordinates you
gava are relative to the screen. You have to offset them with the frame's coordinates
in order that the dialog appear in the middle of the frame.

Here is code that does it:

      public void setVisible(boolean b)
          if (b)
                Rectangle bounds = getParent().getBounds();
                Rectangle abounds = getBounds();

                setLocation(bounds.x + (bounds.width - abounds.width)/ 2,
                       bounds.y + (bounds.height - abounds.height)/2);


Let's summarize 'parent':

- parent concerning modality
- parent container. Usually after parent.add(child).
- parent class in terms of inheritance.

I wouldn't include 'parent class' right now, we have enough confusion.
Better to call that , 'super' class, and keep parent for actual objects.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.