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

public static void centerWindow(Window window, int width, int height) {
    // Get screen size
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension screensize = tk.getScreenSize();

    // Set window minimum size
    window.setMinimumSize(new Dimension((int) Math.floor(screensize.getWidth() * 0.3),
            (int) Math.floor(screensize.getHeight() * 0.3)));

    // Set window size
    if (width == 0f)
        width = (int) Math.floor(screensize.getWidth() * 0.8);

    if (height == 0f)
        height = (int) Math.floor(screensize.getHeight() * 0.8);

    window.setPreferredSize(new Dimension(width, height));

    int left = (int) (screensize.getWidth() - width) / 2;
    int right = (int) (screensize.getHeight() - height) / 2;
    ;/*from  w  w  w.j a  v a 2  s.c o  m*/

    // Center window
    window.setLocation(left, right);
}

From source file:Main.java

/**
 * Returns the screen bounds for a component location (or screen location if component null).
 *///ww w  . j  a  va  2s  . c  o m
public static Rectangle getScreenBounds(Component aComp, int anX, int aY, boolean doInset) {
    GraphicsConfiguration gc = getGraphicsConfiguration(aComp, anX, aY);
    Rectangle rect = gc != null ? gc.getBounds() : new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    Insets ins = doInset && gc != null ? Toolkit.getDefaultToolkit().getScreenInsets(gc) : null;
    if (ins != null) {
        int lt = ins.left, rt = ins.right, tp = ins.top, bt = ins.bottom;
        rect.setBounds(rect.x + lt, rect.y + tp, rect.width - lt - rt, rect.height - tp - bt);
    }
    return rect;
}

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);//from ww  w  .  j  a  va2s .co m
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

public static void copyClipboard(String text) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection selection = new StringSelection(text);
    clipboard.setContents(selection, selection);

    return;//from   w w  w.  j av a2 s .co  m
}

From source file:Main.java

/**
 * Copy a String to clipborad/*w  w  w  . j a  v a2  s. c  o m*/
 *
 * @param string
 */
public static void copyToClipboard(String string) {
    StringSelection stringSelection = new StringSelection(string);
    Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
    clpbrd.setContents(stringSelection, null);
}

From source file:Main.java

/**
 * Gauge the given component on screen./*from  w  ww  .ja v  a2s.  co  m*/
 *
 * @param component Swing-component to gauge.
 */
public static void makeCentered(final Component component) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dim = toolkit.getScreenSize();
    int screenWidth = dim.width;
    int screenHeight = dim.height;
    component.setLocation((screenWidth - component.getSize().width) / 2,
            (screenHeight - component.getSize().height) / 2);
}

From source file:Main.java

/**
 * Don't use this method unless you want all the centerAndPack calls to set the center of the window somewhere other
 * than the center!/*w  w w.  j  av a2  s. co  m*/
 *
 * @return the windowXCenter
 */
public static int getWindowXCenter() {
    if (windowXCenter == -1) {
        windowXCenter = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
    }
    return windowXCenter;
}

From source file:Main.java

/**
 * Displays a message dialog with given information.
 *///  w  w w. java2s  .c o m
public static void showInformationDialog(Component component, String message) {
    final JPanel panel = new JPanel(new BorderLayout(5, 5));

    final JLabel messageLabel = new JLabel(message);
    messageLabel.setFont(new Font("Dialog", Font.BOLD, messageLabel.getFont().getSize()));

    panel.add(messageLabel, BorderLayout.CENTER);

    // Adjust stack trace dimensions
    final Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    screenDimension.setSize(screenDimension.getWidth() * 0.7, screenDimension.getHeight() * 0.7);
    final Dimension maxStackTraceDimension = new Dimension(500, 500);
    maxStackTraceDimension.setSize(Math.min(maxStackTraceDimension.getWidth(), screenDimension.getWidth()),
            Math.min(maxStackTraceDimension.getHeight(), screenDimension.getHeight()));

    JOptionPane.showMessageDialog(component, panel, "Information", JOptionPane.INFORMATION_MESSAGE);
}

From source file:Main.java

public static void center(final Component c) {
    final Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenDimensions.width - c.getWidth()) / 2;
    int y = (screenDimensions.height - c.getHeight()) / 2;
    c.setLocation(x, y);//from   ww w  .j av a 2 s .  co  m
}

From source file:Main.java

/** Repositions a frame to be centered on the screen */
public static void center(Dialog frame) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screen = tk.getScreenSize();
    Dimension win = frame.getSize();
    //frame.setSize(screenWidth / 2, screenHeight / 2);
    frame.setLocation((screen.width - win.width) / 2, (screen.height - win.height) / 2);
}