Solved

setLocation in Dialog

Posted on 1998-10-20
9
456 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 10 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
 
LVL 8

Expert Comment

by:diakov
ID: 1226022
I also experience relative to screen positioning when use this method.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 39
countAbc challenge 9 50
computer science syllabus 3 53
backtracking recursion  code 19 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now