7 Replies Latest reply on Mar 30, 2004 5:49 PM by 807587

    How can I open a JFrame at the center of my screen ?

    807587
      Hi everybody.

      When you open a JFrame, it's usally opened a the top left og the screen. Who can do so that the JFrame appear at the center of the screen ?

      Thank's for any reply.
        • 1. Re: How can I open a JFrame at the center of my screen ?
          807587
          java.awt.Dimension ss=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
          setLocation([count location from ss and getSize() data]);
          • 2. Re: How can I open a JFrame at the center of my screen ?
            807587
            Try
            http://forum.java.sun.com/thread.jsp?forum=57&thread=290275
            http://forum.java.sun.com/thread.jsp?forum=57&thread=371230
            from searching the forum

            My code is here:
                      Dimension d = null; // size of what we're positioning against
                      Point     p = null;
            
                      if (w != null) // w is what we are positioning against, null means desktop
                      {
                        d = w.getSize();
                        p = w..getLocation();
                      }
                      else
                      {
                        d = Toolkit.getDefaultToolkit().getScreenSize();
                        p = new Point();
                      }
            
                      double centreX = p.getX() + d.getWidth()  / 2;
                      double centreY = p.getY() + d.getHeight() / 2;
            
                      this.getSize(d);
                      p.setLocation(centreX - d.getWidth() / 2,
                                    centreY - d.getHeight() / 2);
                      if (p.getX() < 0)
                        p.setLocation(0, p.getY());
            
                      if (p.getY() < 0)
                        p.setLocation(p.getX(), 0);
            
                      this.setLocation(p);
            • 3. Re: How can I open a JFrame at the center of my screen ?
              807587
              //shrinks w,h if too big, moves x,y if off screen
              public static void adjustBounds(JFrame frame) {
                  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                  Rectangle bounds = ge.getMaximumWindowBounds();
                  Rectangle frameBounds = frame.getBounds();
                  if (frameBounds.x < 0)
                      frameBounds.x = 0;
                  if (frameBounds.y < 0)
                      frameBounds.y = 0;
                  if (frameBounds.width >= bounds.width)
                      frameBounds.width = bounds.width;
                  if (frameBounds.height >= bounds.height)
                      frameBounds.height = bounds.height;
                  if (frameBounds.x + frameBounds.width > bounds.width)
                      frameBounds.x = bounds.width - frameBounds.width;
                  if (frameBounds.y + frameBounds.height > bounds.height)
                      frameBounds.y = bounds.height - frameBounds.height;
                  frame.setBounds(frameBounds);
                  if (frameBounds.width == bounds.width && frameBounds.height == bounds.height)
                      frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                  frame.validate();
              }
              I use the GraphicsEnvironment method getMaximumWindowBounds which takes into account things
              like the Window's task bar. I also set the state to maximized if it fills the available area.
              Finally, I use this method in code that is restoring frame bound preferences when an application is
              launched, so I have to consider if the display resolution has been changed or the window was left
              partially off the screen.
              • 4. Re: How can I open a JFrame at the center of my screen ?
                807587
                Woops, wrong method. Here's the centring code:
                public static void center(JFrame frame) {
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        Point center = ge.getCenterPoint();
                        Rectangle bounds = ge.getMaximumWindowBounds();
                        int w = Math.max(bounds.width/2, Math.min(frame.getWidth(), bounds.width));
                        int h = Math.max(bounds.height/2, Math.min(frame.getHeight(), bounds.height));
                        int x = center.x - w/2, y = center.y - h/2;
                        frame.setBounds(x, y, w, h);
                        if (w == bounds.width && h == bounds.height)
                            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                        frame.validate();
                    }
                I usually call this after calling pack(). Your mileage may vary, but I adjust the size of the frame so that
                it is at least half the screen wide and half the screen high (to avoid tiny frames), and not wider or higher
                than the screen (which happens when you pack with a large component inside a JScrollPane).
                • 5. Re: How can I open a JFrame at the center of my screen ?
                  807587
                  Thank's everybody for your your answers. I can now do what I want.
                  • 6. Re: How can I open a JFrame at the center of my screen ?
                    camickr
                    frame.pack() or frame.setSize(...);
                    frame.setLocationRelativeTo( null );
                    frame.setVisible( true );
                    • 7. Re: How can I open a JFrame at the center of my screen ?
                      807587
                      frame.pack() or frame.setSize(...);
                      frame.setLocationRelativeTo( null );
                      frame.setVisible( true );
                      I use this in quick'n'dirty code, but realize that it doesn't take things like the Window's task bar into account,
                      and doesn't maximize the window even if its dimensions justify it.