Java Code Examples for java.awt.Window#setLocation()

The following examples show how to use java.awt.Window#setLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowUtils.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
public static void packAndCenterWindowOn(Window window, Component centeredOn) {
    window.pack();
    Dimension prefSize = window.getPreferredSize();
    Dimension minSize  = window.getMinimumSize();
    int       width    = Math.max(prefSize.width, minSize.width);
    int       height   = Math.max(prefSize.height, minSize.height);
    int       x;
    int       y;
    if (centeredOn != null) {
        Point     where = centeredOn.getLocationOnScreen();
        Dimension size  = centeredOn.getSize();
        x = where.x + (size.width - width) / 2;
        y = where.y + (size.height - height) / 2;
    } else {
        Rectangle bounds = getMaximumWindowBounds(window);
        x = bounds.x + (bounds.width - width) / 2;
        y = bounds.y + (bounds.height - height) / 2;
    }
    window.setLocation(x, y);
    forceOnScreen(window);
}
 
Example 2
Source File: Util.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/** Centers the specified window on the screen. */
public static void center(final Window w) {
  final Dimension screenSize = getScreenSize(w);
  final int screenWidth = screenSize.width;
  final int screenHeight = screenSize.height;
  final int windowWidth = w.getWidth();
  final int windowHeight = w.getHeight();
  if (windowHeight > screenHeight) {
    return;
  }
  if (windowWidth > screenWidth) {
    return;
  }
  final int x = (screenWidth - windowWidth) / 2;
  final int y = (screenHeight - windowHeight) / 2;
  w.setLocation(x, y);
}
 
Example 3
Source File: SeaGlassRootPaneUI.java    From seaglass with Apache License 2.0 6 votes vote down vote up
/**
 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
 */
public void mouseDragged(MouseEvent ev) {
    Window w  = (Window) ev.getSource();
    Point  pt = ev.getPoint();

    if (isMovingWindow) {
        Point eventLocationOnScreen = ev.getLocationOnScreen();

        w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
    } else if (dragCursor != 0) {
        Rectangle r           = w.getBounds();
        Rectangle startBounds = new Rectangle(r);
        Dimension min         = w.getMinimumSize();

        switch (dragCursor) {

        case Cursor.E_RESIZE_CURSOR:
            adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
            break;

        case Cursor.S_RESIZE_CURSOR:
            adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
            break;

        case Cursor.N_RESIZE_CURSOR:
            adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
            break;

        case Cursor.W_RESIZE_CURSOR:
            adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
            break;

        case Cursor.NE_RESIZE_CURSOR:
            adjust(r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX) - r.width, -(pt.y - dragOffsetY));
            break;

        case Cursor.SE_RESIZE_CURSOR:
            adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, pt.y + (dragHeight - dragOffsetY) - r.height);
            break;

        case Cursor.NW_RESIZE_CURSOR:
            adjust(r, min, pt.x - dragOffsetX, pt.y - dragOffsetY, -(pt.x - dragOffsetX), -(pt.y - dragOffsetY));
            break;

        case Cursor.SW_RESIZE_CURSOR:
            adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), pt.y + (dragHeight - dragOffsetY) - r.height);
            break;

        default:
            break;
        }

        if (!r.equals(startBounds)) {
            w.setBounds(r);
            // Defer repaint/validate on mouseReleased unless dynamic
            // layout is active.
            if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
                w.validate();
                getRootPane().repaint();
            }
        }
    }
}
 
Example 4
Source File: LocationByPlatformTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 5
Source File: LocationByPlatformTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 6
Source File: Sage.java    From sagetv with Apache License 2.0 5 votes vote down vote up
private static void splashAndLicense()
{
  splashWindow = new Window(masterWindow);
  splashWindow.setLayout(new BorderLayout());
  Image theImage = null;
  String splashImageName;
  if (Sage.get("ui/splash_image", null) != null)
  {
    theImage = Toolkit.getDefaultToolkit().createImage(Sage.get("ui/splash_image", null));
    ImageUtils.ensureImageIsLoaded(theImage);
  }
  else
  {
    theImage = ImageUtils.fullyLoadImage(isTrueClient() ? (is64BitJVM() ? "images/splashclient64.gif" : "images/splashclient.gif") : 
                                                          (is64BitJVM() ? "images/splash64.gif"       : "images/splash.gif"      ));
  }
  ActiveImage splashImage = new ActiveImage(theImage);
  splashWindow.add(splashImage, "Center");
  splashText = new Label(Sage.rez("Module_Init", new Object[] { "Application" }), Label.CENTER)
  {
    public void paint(Graphics g)
    {
      super.paint(g);
      g.setColor(Color.black);
      g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    }
  };
  splashText.setBackground(new Color(42, 103, 190));
  splashText.setForeground(Color.white);
  splashWindow.add(splashText, "South");
  Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
  splashWindow.pack();
  Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
  splashWindow.setLocation(center.x - splashWindow.getWidth()/2, center.y - splashWindow.getHeight()/2);
  splashWindow.setVisible(true);
}
 
Example 7
Source File: GraphicUtils.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Centers the window over a component (usually another window). The window
    * must already have been sized.
    * 
    * @param window
    *            Window to center.
    * @param over
    *            Component to center over.
    */
   public static void centerWindowOnComponent(Window window, Component over) {
if ((over == null) || !over.isShowing()) {
    centerWindowOnScreen(window);
    return;
}

Point parentLocation = over.getLocationOnScreen();
Dimension parentSize = over.getSize();
Dimension size = window.getSize();

// Center it.
int x = parentLocation.x + (parentSize.width - size.width) / 2;
int y = parentLocation.y + (parentSize.height - size.height) / 2;

// Now, make sure it's onscreen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

// This doesn't actually work on the Mac, where the screen
// doesn't necessarily start at 0,0
if (x + size.width > screenSize.width)
    x = screenSize.width - size.width;

if (x < 0)
    x = 0;

if (y + size.height > screenSize.height)
    y = screenSize.height - size.height;

if (y < 0)
    y = 0;

window.setLocation(x, y);
   }
 
Example 8
Source File: UIUtilities.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
public static void centerOnScreen(Window window) {
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension size = window.getSize();
  window.setLocation(
    (screenSize.width - size.width) / 2,
    (screenSize.height - size.height) / 2);
}
 
Example 9
Source File: SwingUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static void centerOnParent(Window win) {
	Container parent = win.getParent();

	if (parent != null) {
		Point parentPosition = parent.getLocationOnScreen();
		win.setLocation(parentPosition.x + (parent.getWidth() - win.getWidth()) / 2,
				parentPosition.y + (parent.getHeight() - win.getHeight()) / 2);
	} else {
		centerOnScreen(win);
	}
}
 
Example 10
Source File: UIUtil.java    From Astrosoft with GNU General Public License v2.0 5 votes vote down vote up
public static void setWindowLocation(Window window, Component parentComponent) {
	
	Point comboLocation =
		parentComponent.getLocationOnScreen(  );
       Dimension size = parentComponent.getSize(  );
       Dimension windowSize = window.getSize(  );
       Dimension screenSize =
           Toolkit.getDefaultToolkit(  ).getScreenSize(  );

       if ( 
           ( ( comboLocation.x
               - ( windowSize.width - size.width ) ) <= 0 )
               && ( ( comboLocation.y + size.height
               + windowSize.height ) >= screenSize.height ) ) {
           window.setLocation( 
               0, comboLocation.y - windowSize.height );
       } else if ( 
           ( comboLocation.x
               - ( windowSize.width - size.width ) ) <= 0 ) {
           window.setLocation( 
               0, comboLocation.y + size.height );
       } else if ( 
           ( comboLocation.y + size.height
               + windowSize.height ) >= screenSize.height ) {
           window.setLocation( 
               comboLocation.x
               - ( windowSize.width - size.width ),
               comboLocation.y - windowSize.height );
       } else {
           window.setLocation( 
               comboLocation.x
               - ( windowSize.width - size.width ),
               comboLocation.y + size.height );
       }
}
 
Example 11
Source File: WinCenter.java    From Java with Artistic License 2.0 5 votes vote down vote up
public static void center(Window win){
	Toolkit tkit = Toolkit.getDefaultToolkit();
	Dimension sSize = tkit.getScreenSize();
	Dimension wSize = win.getSize();
	if(wSize.height > sSize.height){
		wSize.height = sSize.height;
	}
	if(wSize.width > sSize.width){
		wSize.width = sSize.width;
	}
	win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
}
 
Example 12
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
    * Centers the window over a component (usually another window). The window
    * must already have been sized.
    * 
    * @param window
    *            Window to center.
    * @param over
    *            Component to center over.
    */
   public static void centerWindowOnComponent(Window window, Component over) {
if ((over == null) || !over.isShowing()) {
    centerWindowOnScreen(window);
    return;
}

Point parentLocation = over.getLocationOnScreen();
Dimension parentSize = over.getSize();
Dimension size = window.getSize();

// Center it.
int x = parentLocation.x + (parentSize.width - size.width) / 2;
int y = parentLocation.y + (parentSize.height - size.height) / 2;

// Now, make sure it's onscreen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

// This doesn't actually work on the Mac, where the screen
// doesn't necessarily start at 0,0
if (x + size.width > screenSize.width)
    x = screenSize.width - size.width;

if (x < 0)
    x = 0;

if (y + size.height > screenSize.height)
    y = screenSize.height - size.height;

if (y < 0)
    y = 0;

window.setLocation(x, y);
   }
 
Example 13
Source File: DialogLoggerUtil.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Center on parent ( absolute true/false (exact center or 25% upper left) )
 * 
 * @param child
 * @param absolute
 */
public static void centerOnParent(final Window child, final boolean absolute) {
  child.pack();
  boolean useChildsOwner = child.getOwner() != null
      ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
      : false;
  final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
  final Point parentLocationOnScreen =
      useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0, 0);
  final Dimension childSize = child.getSize();
  childSize.width = Math.min(childSize.width, screenSize.width);
  childSize.height = Math.min(childSize.height, screenSize.height);
  child.setSize(childSize);
  int x;
  int y;
  if ((child.getOwner() != null) && child.getOwner().isShowing()) {
    x = (parentSize.width - childSize.width) / 2;
    y = (parentSize.height - childSize.height) / 2;
    x += parentLocationOnScreen.x;
    y += parentLocationOnScreen.y;
  } else {
    x = (screenSize.width - childSize.width) / 2;
    y = (screenSize.height - childSize.height) / 2;
  }
  if (!absolute) {
    x /= 2;
    y /= 2;
  }
  child.setLocation(x, y);
}
 
Example 14
Source File: PDialog.java    From PolyGlot with MIT License 5 votes vote down vote up
/**
 * Sets window visibly to the right of the window handed in
 *
 * @param w window to set location relative to
 */
public void setBeside(final Window w) {
    final Window self = this;
    skipCenter = true;
    
    int x = w.getLocation().x + w.getWidth();
    int y = w.getLocation().y;

    self.setLocation(x, y);
}
 
Example 15
Source File: LocationByPlatformTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 16
Source File: LocationByPlatformTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 17
Source File: Util.java    From drmips with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Centers the given window on the screen.
 * @param window Window to center.
 */
public static void centerWindow(Window window) {
	Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	window.setLocation((int)screen.getWidth() / 2 - window.getWidth() / 2, (int)screen.getHeight() / 2 - window.getHeight() / 2);
}
 
Example 18
Source File: LizziePane.java    From lizzie with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  Window w = (Window) e.getSource();
  Point pt = e.getPoint();

  if (isMovingWindow) {
    Point eventLocationOnScreen = e.getLocationOnScreen();
    w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
  } else if (dragCursor != 0) {
    Rectangle r = w.getBounds();
    Rectangle startBounds = new Rectangle(r);
    Dimension min = w.getMinimumSize();

    switch (dragCursor) {
      case Cursor.E_RESIZE_CURSOR:
        adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
        break;
      case Cursor.S_RESIZE_CURSOR:
        adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      case Cursor.N_RESIZE_CURSOR:
        adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
        break;
      case Cursor.W_RESIZE_CURSOR:
        adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
        break;
      case Cursor.NE_RESIZE_CURSOR:
        adjust(
            r,
            min,
            0,
            pt.y - dragOffsetY,
            pt.x + (dragWidth - dragOffsetX) - r.width,
            -(pt.y - dragOffsetY));
        break;
      case Cursor.SE_RESIZE_CURSOR:
        adjust(
            r,
            min,
            0,
            0,
            pt.x + (dragWidth - dragOffsetX) - r.width,
            pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      case Cursor.NW_RESIZE_CURSOR:
        adjust(
            r,
            min,
            pt.x - dragOffsetX,
            pt.y - dragOffsetY,
            -(pt.x - dragOffsetX),
            -(pt.y - dragOffsetY));
        break;
      case Cursor.SW_RESIZE_CURSOR:
        adjust(
            r,
            min,
            pt.x - dragOffsetX,
            0,
            -(pt.x - dragOffsetX),
            pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      default:
        break;
    }
    if (!r.equals(startBounds)) {
      w.setBounds(r);
      // Defer repaint/validate on mouseReleased unless dynamic
      // layout is active.
      if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
        w.validate();
        getRootPane().repaint();
      }
    }
  }
}
 
Example 19
Source File: TN5250jSplashScreen.java    From tn5250j with GNU General Public License v2.0 4 votes vote down vote up
/**
    * Creates the Splash screen window and configures it.
    */
   protected void initialize(ImageIcon iimage) {

      image = iimage.getImage();
      // if no image, return
      if (image == null) {
         throw new IllegalArgumentException("Image specified is invalid.");
      }
//      System.out.println(" here in splash ");

      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(image,0);

      try {
         tracker.waitForAll();
      }
      catch(Exception e) {
         System.out.println(e.getMessage());
      }

      // create dialog window
      f = new Frame();
      dialog = new Window(f);
      dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      Dimension s = new Dimension(image.getWidth(this) + 2,
         image.getHeight(this) + 2);
      setSize(s);
      dialog.setLayout(new BorderLayout());
      dialog.setSize(s);

      dialog.add(this,BorderLayout.CENTER);
      dialog.pack();

      // position splash screen
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      int x = (screen.width - s.width)/2;
      if (x < 0) {
         x = 0;
      }

      int y = (screen.height - s.height)/2;
      if (y < 0) {
         y = 0;
      }

      dialog.setLocation(x, y);
      dialog.validate();

   }
 
Example 20
Source File: GuiUtils.java    From sc2gears with Apache License 2.0 2 votes vote down vote up
/**
 * Centers a window relative to another window.
 * @param window   window to be centered
 * @param toWindow reference window to be centered to
 */
public static void centerWindowToWindow( final Window window, final Window toWindow ) {
	window.setLocation(	toWindow.getX() + ( toWindow.getWidth() - window.getWidth() ) / 2, toWindow.getY() + ( toWindow.getHeight() - window.getHeight() ) / 2 );
}