Solved

setLocation in Dialog

Posted on 1998-10-20
9
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
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

739 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