Java Window show(final Container container, final boolean visible, Window appearOnLeftSide)

Here you can find the source of show(final Container container, final boolean visible, Window appearOnLeftSide)

Description

show

License

Open Source License

Declaration

public static void show(final Container container, final boolean visible, Window appearOnLeftSide) 

Method Source Code

//package com.java2s;
/**/*from  ww w  .  j av a2  s .c o  m*/
 * @(#)GUIUtil.java 1.0 26.09.06 (dd.mm.yy)
 *
 * Copyright (2003) Bro3
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2, or any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Boston, MA 02111.
 * 
 * Contact: bro3@users.sourceforge.net
 **/

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.SwingUtilities;

public class Main {
    public static void show(final java.awt.Container container, final boolean visible) {
        show(container, visible, null);
    }

    public static void show(final Container container, final boolean visible, Window appearOnLeftSide) {

        if (visible)
            adjustLocation(container, appearOnLeftSide);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                container.setVisible(visible);
            }
        });
    }

    private static void adjustLocation(Container container, Window appearOnLeftSide) {

        Point p = container.getLocation();

        Dimension size = container.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int widthLocation = (int) (p.getX() + size.getWidth());
        int heightLocation = (int) (p.getY() + size.getHeight());

        if (appearOnLeftSide != null) {

            Point appearLeft = appearOnLeftSide.getLocation();

            int wSize = (int) size.getWidth();
            p.setLocation(appearLeft.getX() - wSize, p.getY());
        }

        if (widthLocation > screenSize.getWidth()) {
            int diff = (int) (widthLocation - screenSize.getWidth());
            p.setLocation((p.getX() - diff), p.getY());
        }

        if (heightLocation > screenSize.getHeight()) {
            int diff = (int) (heightLocation - screenSize.getHeight());
            p.setLocation(p.getX(), (p.getY() - diff));
        }

        if (p.getX() < 0) {
            p.setLocation(0.0, p.getY());
        }

        if (p.getY() < 10) {
            p.setLocation(p.getX(), 15.0);
        }

        container.setLocation(p);
    }

    public static void invokeLater(Runnable runnable) {

        if (!SwingUtilities.isEventDispatchThread())
            SwingUtilities.invokeLater(runnable);
        else
            runnable.run();
    }
}

Related

  1. putWindowNormalBounds(Window window, Rectangle bounds)
  2. removeWindow(Window w)
  3. repaintMnemonicsInWindow(Window w)
  4. setWindowAlpha(Window w, int value)
  5. setWindowOpacity(Window window, float opacity)
  6. show(JWindow window)
  7. showLater(final Window win)
  8. showMessageWindow(String message, JWindow parent)
  9. unmaskWindow(W window)