Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

setLocation in Dialog

Posted on 1998-10-20
9
Medium Priority
?
475 Views
Last Modified: 2012-08-13
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.

Thanks.
0
Comment
Question by:omok
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1226019
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.
0
 
LVL 1

Author Comment

by:omok
ID: 1226020
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...
0
 
LVL 1

Accepted Solution

by:
mco earned 40 total points
ID: 1226021
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:diakov
ID: 1226022
I also experience relative to screen positioning when use this method.
0
 
LVL 1

Expert Comment

by:mco
ID: 1226023
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.

0
 
LVL 1

Author Comment

by:omok
ID: 1226024
mco,
>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);

0
 
LVL 1

Expert Comment

by:mco
ID: 1226025
omok,
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);
          }

            super.setVisible(b);
      }

0
 
LVL 8

Expert Comment

by:diakov
ID: 1226026
Let's summarize 'parent':

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

Cheers,
  Nik
0
 
LVL 1

Expert Comment

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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question