Solved

setLocation in Dialog

Posted on 1998-10-20
9
463 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 81
tomcat startup error 5 65
Error in @AspectJ Based AOP with Spring 2 13
Java syntax, or is it Selenium 6 14
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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