we want the complete dialog on screen - Java Swing

Java examples for Swing:JComponent

Description

we want the complete dialog on screen

Demo Code

/*****************************************************************************************
 *
 *  This file is part of PlanckDB.//w w w. j av  a 2 s.  co m
 *
 *  PlanckDB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PlanckDB 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with PlanckDB.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (C) Gidon Shabat, 2008
 *
 *  Authors: Gidon Shabat <gidi@planckdb.com>
 *  Date:    06/16/2011
 *
 *******************************************************************************************/
//package com.java2s;

import java.awt.*;

public class Main {
    /**
     * we want the complete dialog on screen
     *
     */
    public static void setPreferredLocation(Component component) {
        Point loc = MouseInfo.getPointerInfo().getLocation();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        loc.x = (loc.x - component.getWidth() / 2);
        loc.y = (loc.y - component.getHeight() / 2);
        if (loc.x + component.getWidth() > dim.getWidth()) {
            loc.x = (int) (dim.getWidth() - component.getWidth());
        }
        if (loc.x < 0) {
            loc.x = 0;
        }
        if (loc.y + component.getHeight() > dim.getHeight()) {
            loc.y = (int) (dim.getHeight() - component.getHeight());
        }
        if (loc.y < 0) {
            loc.y = 0;
        }
        component.setLocation(loc);
    }
}

Related Tutorials