Example usage for java.awt Toolkit getDefaultToolkit

List of usage examples for java.awt Toolkit getDefaultToolkit

Introduction

In this page you can find the example usage for java.awt Toolkit getDefaultToolkit.

Prototype

public static synchronized Toolkit getDefaultToolkit() 

Source Link

Document

Gets the default toolkit.

Usage

From source file:Main.java

/**
 * This method returns the point at which the incoming dialog will be
 * perfectly centered on screen./*ww w.j  a  va  2  s .  co  m*/
 * @param windowToCenter The window to center. Make sure thesize has been set (pack or validate first).
 * @return The point to set on the dialog using setLocation().
 */
public static Point getCenteredWindowPoint(java.awt.Window windowToCenter) {
    //Center the Dialog
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = windowToCenter.getSize();
    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }
    Point centerCoord = new Point((screenSize.width - frameSize.width) / 2,
            (screenSize.height - frameSize.height) / 2);
    return centerCoord;
}

From source file:Main.java

/**
 * Get the foreground color for the current UI.
 * @return The desired foreground color.
 *//*from   w  w w.j  av  a 2 s. co m*/
public static Color getSelectionForegroundColor() {
    if (selectionForegroundColor != null) {
        return selectionForegroundColor;
    }

    //for windows
    selectionForegroundColor = (Color) Toolkit.getDefaultToolkit()
            .getDesktopProperty("win.item.highlightTextColor");
    if (selectionForegroundColor == null) {
        selectionForegroundColor = UIManager.getColor("Table.selectionForeground");
        if (selectionForegroundColor == null) {
            selectionForegroundColor = UIManager.getColor("Table[Enabled+Selected].textForeground");
            if (selectionForegroundColor == null) {
                selectionForegroundColor = new JList().getSelectionForeground();
            }
        }

        //sometimes the UIManager color won't work
        selectionForegroundColor = new Color(selectionForegroundColor.getRed(),
                selectionForegroundColor.getGreen(), selectionForegroundColor.getBlue());
    }

    return selectionForegroundColor;
}

From source file:Main.java

/**
 * Centers a {@link JFrame} on screen.// ww w  .  jav  a 2  s .  c o m
 */
public static void centerFrameOnScreen(JFrame frame) {
    final Dimension position = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((position.width - frame.getWidth()) / 2, (position.height - frame.getHeight()) / 2);
}

From source file:Main.java

public static void copyTextToClipboard(String data) {
    StringSelection selection = new StringSelection(data);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
}

From source file:Main.java

/** Returns the Screen resolution. If there're more than one display you might want to use: {@link #getScreenSizes()} 
 * @return the Screen resolution/*ww w.  j  a v a2s.c om*/
 * @see #getScreenSizes()
 */
public static Dimension getScreenSize() {
    return Toolkit.getDefaultToolkit().getScreenSize();
}

From source file:Main.java

public static void centerOnScreen(Window window) {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = window.getSize();

    int x = (screenSize.width - windowSize.width) / 2;
    int y = (screenSize.height - windowSize.height) / 2;

    window.setLocation(x, y);//www  .j  a v a2 s .  com
}

From source file:Main.java

/**
 * Adds a global Keylistener which receive all KeyEvents which are sent.</br> Code
 * Example:</br> <code><pre>
 * AWTEventListener ael = new AWTEventListener() { 
 *    public void eventDispatched(AWTEvent e ) { 
 *       if(e.getID() == KeyEvent.KEY_PRESSED){
 *          doSomeThing(); //eg. keyPressed((KeyEvent) e);  
 *       }  //  w  w w.j a  va  2  s . c  om
 *    }
 * };
 * </pre>
 *</code>
 * 
 * @param listener to add
 */
public static void addGlobalKeyListener(AWTEventListener listener) {
    Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
}

From source file:Main.java

/**
 * Center JDialog./*w  w  w. j a  v  a  2s  .  c  o  m*/
 */
public static void center(JDialog dialog) {
    Dimension dialogSize = dialog.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    dialog.setLocation((screenSize.width - dialogSize.width) >> 1,
            (screenSize.height - dialogSize.height) >> 1);
}

From source file:Main.java

/**
 * Position the given component at the center of the given parent component or physical screen.
 * //ww  w .  java 2  s  . co  m
 * @param c the component to be positioned
 * @param parent the component whose center will match the center of the given component.
 * If null, the given component will match the screen center.
 * 
 */
public static void position(Component c, Component parent) {
    Dimension d = c.getPreferredSize();
    if (parent == null) {
        Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
        c.setLocation(s.width / 2 - d.width / 2, s.height / 2 - d.height / 2);
    } else {
        Point p = parent.getLocationOnScreen();
        int pw = parent.getWidth();
        int ph = parent.getHeight();
        c.setLocation(p.x + pw / 2 - d.width / 2, p.y + ph / 2 - d.height / 2);
    }
}

From source file:Main.java

public static void beep() {
    Toolkit.getDefaultToolkit().beep();
}