Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is there a way to specify where on screen a JFrame or a JDialog should appear?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Is there a way to specify where on screen a JFrame or a JDialog should appear?

    The title says it all.

    I've got a program that uses a few JFrames and JDialogs. The main window is a JFrame and always appears in the top left corner of the screen. The same applies to all other JFrames I use. In contrast, a JDialog that I use appears in the middle of the screen (both X and Y). I think this is rather "ugly". How can I change this.

    The nicest would be if I could specify that every JFrame and JDialog that are not the main window should appear in the middle of the main window JFrame (both X and Y).

    If that is not possible, I would like to specify that every JFrame appears in the middle of the screen.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 688 Times in 675 Posts

    Default Re: Is there a way to specify where on screen a JFrame or a JDialog should appear?

    Your first resource to answer these kind of "How do I . . . ?" questions should always be the APIs. Review the methods, think of the possibilities as you read their descriptions, then explore how to use them in code. As you look at the API pages for JDialog, JFrame, probably all of the top-level containers, do you see any methods that control placement of the container on the screen? You should.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Is there a way to specify where on screen a JFrame or a JDialog should appear?

    Thanks. That was useful advice. It was a bit of searching between all the different methods of JFrame and its friends, but I found it.

    The location of a JOptionpane (which I use quite a lot) can be set in its constructor. The location of a JFrame can be set using the methods setLocation and setLocationRelativeTo.

    However, I've still got a question. setLocationRelativeTo can be used to center a JFrame relative to another component (or to the screen). However, what happens is that the top-left corner of the JFrame is centered relative to the other component, not the center of the JFrame.

    So I need the following code to center the center of the JFrame relative to another component:

        public class DemoFrame extends JFrame  {
     
            DemoFrame(Component c) {
                super();
                setLocationRelativeTo(c);
                // ...
                // now construct the main frame
                // ...
                setVisible(true);
                Point p = getLocation();
                int xPos = (int) p.getX() - ( getWidth() / 2 );
                int yPos = (int) p.getY() - ( getHeight() / 2 );
                setLocation(xPos, yPos);
           }
      }

    This works. But it looks a bit odd. I need to set the location twice, first in order to determine where the window would be using the normal setLocationRelativeTo (i.e. centered with its top-left corner) and then to properly set it centered (i.e. centered with its center). Isn't there a more simple solution?

    Then I discovered an oddity. If I call "setLocationRelativeTo(c)" followed by "p = getLocation()" directly at the beginning of the constructor, it returns different values than when I call "setLocationRelativeTo(c)" at the end (i.e. directly before the statement "Point p = getLocation()". For example, for a JFrame in the program I am writing:

    at the beginning of the constructor: p.getX(): 960.0 , p.getY(): 525.0
    at the end of the constructor: p.getX(): X: 875.0 , p.getY(): 465.0

    Does anyone have an idea why?

  4. The Following User Says Thank You to evert67 For This Useful Post:

    GregBrannon (October 10th, 2014)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 688 Times in 675 Posts

    Default Re: Is there a way to specify where on screen a JFrame or a JDialog should appear?

    Great follow-up post.

    It seems the first setLocation call puts it roughly where you want it, and the second call fine tunes it or shifts it to where you really want it. That's my sense, but I haven't verified it by running variations of your code.

  6. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Is there a way to specify where on screen a JFrame or a JDialog should appear?

    Well, I discovered the reason...

    One can easily center a JFrame such that its center coincides with the center of the component c that creates it. Simply use the statement "setLocationRelativeTo(c)" or "setLocationRelativeTo(null)" for the first JFrame displayed.

    However... if setLocationRelativeTo is called in the beginning of the JFrame code, the JFrame has not been created yet. It then has a size of 0 x 0 and its center coincides with its top-left corner. If setLocationRelativeTo is called at the end of the JFrame code, when the JFrame has been created (or at least has its final size) it has a different size and a different center. So only now its center will be correctly overlapping with the center of c.

    Moral of the lesson? Only call setLocationRelativeTo once the JFrame has its final size.

  7. The Following User Says Thank You to evert67 For This Useful Post:

    GregBrannon (October 10th, 2014)

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 688 Times in 675 Posts

    Default Re: Is there a way to specify where on screen a JFrame or a JDialog should appear?

    Of course. One of the many tidbits buried too deep in my head to surface right away. Thanks for explaining.

Similar Threads

  1. How to set JFrame and JDialog to appear centered on the screen
    By Steven1983 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 7th, 2014, 07:28 AM
  2. Need some help with this JFrame, or JDialog.
    By MR bruto in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 10th, 2013, 08:14 AM
  3. different between japplet, jframe, jdialog, jwindow, jpanel
    By hwoarang69 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 27th, 2012, 10:53 PM
  4. Replies: 1
    Last Post: April 30th, 2012, 08:16 AM
  5. JDialog from JFrame button (GUI)
    By Jameseyboy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2012, 07:55 AM