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

/**
 * Closes the given {@link JFrame}./*  w  w w  . ja v a2s  .c o m*/
 * 
 * @param win
 *            - the frame to close
 */
public static void kill(JFrame win) {
    if (win == null) {
        return;
    }
    WindowEvent close = new WindowEvent(win, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
}

From source file:Main.java

/**
 * Returns the image pointed at by the given urlString
 *
 * @param urlString the location of the image
 * @return the image/*  ww w. j  a  va2 s. c  om*/
 * @throws MalformedURLException
 */
public static Image getImageFromUrl(String urlString) throws MalformedURLException {
    URL url = new URL(urlString);
    return Toolkit.getDefaultToolkit().createImage(url);
}

From source file:Main.java

public static String getClipboardData() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    Transferable contents = clipboard.getContents(null);
    DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
    DataFlavor dfTxt = DataFlavor.stringFlavor;

    boolean hasTransferableRTFText = (contents != null) && contents.isDataFlavorSupported(dfRTF);

    boolean hasTransferableTxtText = (contents != null) && contents.isDataFlavorSupported(dfTxt);

    if (hasTransferableRTFText) {
        try {/*from  w w w  .jav  a 2s. c o  m*/
            result = streamToString((InputStream) contents.getTransferData(dfRTF));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (hasTransferableTxtText) {
        try {
            result = (String) contents.getTransferData(dfTxt);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

private static int keyStrokeModMac(String keyStrokeStr) {
    keyStrokeStr = keyStrokeStr.toLowerCase();
    int mod = 0;//from   w  ww .jav a2s  . c o m
    if (keyStrokeStr.contains("ctrl") || keyStrokeStr.contains("control")) {
        mod = mod | InputEvent.CTRL_MASK;
    }
    if (keyStrokeStr.contains("alt")) {
        mod = mod | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
    if (keyStrokeStr.contains("meta")) {
        mod = mod | InputEvent.ALT_MASK;
    }
    return mod;
}

From source file:Main.java

public static void centralizeComponent(Component component, Component otherComponent) {

    Dimension othersDimension = null;
    Point othersLocation = null;/*from  w  w w. j a v a 2s  . c o m*/
    if (otherComponent == null || !otherComponent.isVisible()) {
        othersDimension = Toolkit.getDefaultToolkit().getScreenSize();
        othersLocation = new Point(0, 0);
    } else {
        othersDimension = otherComponent.getSize();
        othersLocation = otherComponent.getLocation();
    }
    Point centerPoint = new Point(
            (int) Math.round(othersDimension.width / HALF - component.getWidth() / HALF) + othersLocation.x,
            (int) Math.round(othersDimension.height / HALF - component.getHeight() / HALF) + othersLocation.y);
    component.setLocation(centerPoint);
}

From source file:Main.java

/**
 * Forces the current thread to yield until the given image has completely
 * loaded. This is useful if you need to guarantee that an image has fully
 * loaded.//from   w ww.ja v  a2  s  . com
 * 
 * @param in
 *            the image being loaded
 * @deprecated Use a java.awt.MediaTracker to watch your image loading
 */
@Deprecated
public static void blockUntilImagePrepared(Image in) {
    while (!Toolkit.getDefaultToolkit().prepareImage(in, -1, -1, null)) {
        Thread.currentThread();
        Thread.yield();
    }
}

From source file:Main.java

/**
 * Returns a <code>Point</code> used as location for a window that
 * should be centered on the screen./*from  w  w  w.j a va  2  s . c  o  m*/
 * 
 * @param size a <code>Dimension</code> describing the window's size
 * @return a <code>Point</code> describing the top-left position
 */
public static Point getCenteredLocation(Dimension size) {

    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension screenSize = tk.getScreenSize();

    return new Point(((screenSize.width - size.width) / 2), ((screenSize.height - size.height) / 2));
}

From source file:Main.java

public static void showPopupMenu(final JPopupMenu popup, final Component component, int x, int y) {
    final Point p = new Point(x, y);
    SwingUtilities.convertPointToScreen(p, component);
    final Dimension size = popup.getPreferredSize();
    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    boolean horiz = false;
    boolean vert = false;

    final int origX = x;

    if ((p.x + size.width > screen.width) && (size.width < screen.width)) {
        x += (screen.width - p.x - size.width);
        horiz = true;/*  w  w  w .ja v a2s.c o m*/
    }

    if ((p.y + size.height > screen.height) && (size.height < screen.height)) {
        y += (screen.height - p.y - size.height);
        vert = true;
    }

    if (horiz && vert) {
        x = origX - size.width - 2;
    }
    popup.show(component, x, y);
}

From source file:Main.java

public static void centerFrame(JDialog jd) {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = jd.getSize();
    jd.setLocation((screenSize.width / 2) - (frameSize.width / 2),
            (screenSize.height / 2) - (frameSize.height / 2));
}

From source file:Main.java

static public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();/*www . j  av a  2 s  .com*/
    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);
}