Example usage for org.eclipse.swt.widgets Display getPrimaryMonitor

List of usage examples for org.eclipse.swt.widgets Display getPrimaryMonitor

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display getPrimaryMonitor.

Prototype

public Monitor getPrimaryMonitor() 

Source Link

Document

Returns the primary monitor for that device.

Usage

From source file:Snippet120.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);//  www  .jav a2 s .c  o  m
    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ShellCenter.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);//from   www. ja v a2s.com

    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();

    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;

    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet120.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 120");
    shell.setSize(200, 200);//from w  w  w. j  a v  a  2s  .  com
    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet373.java

@SuppressWarnings("restriction")
public static void main(String[] args) {
    System.setProperty("swt.autoScale", "quarter");
    Display display = new Display();
    final Image eclipse = new Image(display, filenameProvider);
    final Image eclipseToolBar1 = new Image(display, filenameProvider);
    final Image eclipseToolBar2 = new Image(display, filenameProvider);
    final Image eclipseTableHeader = new Image(display, filenameProvider);
    final Image eclipseTableItem = new Image(display, filenameProvider);
    final Image eclipseTree1 = new Image(display, filenameProvider);
    final Image eclipseTree2 = new Image(display, filenameProvider);
    final Image eclipseCTab1 = new Image(display, filenameProvider);
    final Image eclipseCTab2 = new Image(display, filenameProvider);

    Shell shell = new Shell(display);
    shell.setText("Snippet 373");
    shell.setImage(eclipse);//from   w  ww  .j a  va  2  s.c  o  m
    shell.setText("DynamicDPI @ " + DPIUtil.getDeviceZoom());
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    shell.setLocation(100, 100);
    shell.setSize(500, 600);
    shell.addListener(SWT.ZoomChanged, new Listener() {
        @Override
        public void handleEvent(Event e) {
            if (display.getPrimaryMonitor().equals(shell.getMonitor())) {
                MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL);
                box.setText(shell.getText());
                box.setMessage("DPI changed, do you want to exit & restart ?");
                e.doit = (box.open() == SWT.OK);
                if (e.doit) {
                    shell.close();
                    System.out.println("Program exit.");
                }
            }
        }
    });

    // Menu
    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);
    MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
    fileItem.setText("&File");
    fileItem.setImage(eclipse);
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    MenuItem subItem = new MenuItem(submenu, SWT.PUSH);
    subItem.addListener(SWT.Selection, e -> System.out.println("Select All"));
    subItem.setText("Select &All\tCtrl+A");
    subItem.setAccelerator(SWT.MOD1 + 'A');
    subItem.setImage(eclipse);

    // CTabFolder
    CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(folder, i % 2 == 0 ? SWT.CLOSE : SWT.NONE);
        cTabItem.setText("Item " + i);
        Text textMsg = new Text(folder, SWT.MULTI);
        textMsg.setText("Content for Item " + i);
        cTabItem.setControl(textMsg);
        cTabItem.setImage((i % 2 == 1) ? eclipseCTab1 : eclipseCTab2);
    }

    // PerMonitorV2 setting
    //      Label label = new Label (shell, SWT.BORDER);
    //      label.setText("PerMonitorV2 value before:after:Error");
    //      Text text = new Text(shell, SWT.BORDER);
    //      text.setText(DPIUtil.BEFORE + ":" + DPIUtil.AFTER + ":" + DPIUtil.RESULT);

    // Composite for Label, Button, Tool-bar
    Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout(SWT.HORIZONTAL));

    // Label with Image
    Label label1 = new Label(composite, SWT.BORDER);
    label1.setImage(eclipse);

    // Label with text only
    Label label2 = new Label(composite, SWT.BORDER);
    label2.setText("No Image");

    // Button with text + Old Image Constructor
    Button oldButton1 = new Button(composite, SWT.PUSH);
    oldButton1.setText("Old Img");
    oldButton1.setImage(new Image(display, IMAGE_PATH_100));

    // Button with Old Image Constructor
    //      Button oldButton2 = new Button(composite, SWT.PUSH);
    //      oldButton2.setImage(new Image(display, filenameProvider.getImagePath(100)));

    // Button with Image
    Button createDialog = new Button(composite, SWT.PUSH);
    createDialog.setText("Child Dialog");
    createDialog.setImage(eclipse);
    createDialog.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE);
            dialog.setText("Child Dialog");
            RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
            dialog.setLayout(rowLayout);
            Label label = new Label(dialog, SWT.BORDER);
            label.setImage(eclipse);
            Point location = shell.getLocation();
            dialog.setLocation(location.x + 250, location.y + 50);
            dialog.pack();
            dialog.open();
        }
    });

    // Toolbar with Image
    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.BORDER);
    Rectangle clientArea = shell.getClientArea();
    toolBar.setLocation(clientArea.x, clientArea.y);
    for (int i = 0; i < 2; i++) {
        int style = i % 2 == 1 ? SWT.DROP_DOWN : SWT.PUSH;
        ToolItem toolItem = new ToolItem(toolBar, style);
        toolItem.setImage((i % 2 == 0) ? eclipseToolBar1 : eclipseToolBar2);
        toolItem.setEnabled(i % 2 == 0);
    }
    toolBar.pack();

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Refresh-Current Monitor : Zoom");
    Text text1 = new Text(shell, SWT.BORDER);
    Monitor monitor = button.getMonitor();
    text1.setText("" + monitor.getZoom());
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor monitor = button.getMonitor();
            text1.setText("" + monitor.getZoom());
        }
    });
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Refresh-Both Monitors : Zoom");
    Text text2 = new Text(shell, SWT.BORDER);
    Monitor[] monitors = display.getMonitors();
    StringBuilder text2String = new StringBuilder();
    for (int i = 0; i < monitors.length; i++) {
        text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
    }
    text2.setText(text2String.toString());
    button2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent e) {
            Monitor[] monitors = display.getMonitors();
            StringBuilder text2String = new StringBuilder();
            for (int i = 0; i < monitors.length; i++) {
                text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : ""));
            }
            text2.setText(text2String.toString());
        }
    });

    // Table
    Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    String titles[] = { "Title 1" };
    for (int i = 0; i < titles.length; i++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(titles[i]);
        column.setImage(eclipseTableHeader);
    }
    for (int i = 0; i < 1; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, "Data " + i);
        item.setImage(0, eclipseTableItem);
    }
    for (int i = 0; i < titles.length; i++) {
        table.getColumn(i).pack();
    }

    // Tree
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 1; i++) {
        TreeItem iItem = new TreeItem(tree, 0);
        iItem.setText("TreeItem (0) -" + i);
        iItem.setImage(eclipseTree1);
        TreeItem jItem = null;
        for (int j = 0; j < 1; j++) {
            jItem = new TreeItem(iItem, 0);
            jItem.setText("TreeItem (1) -" + j);
            jItem.setImage(eclipseTree2);
            jItem.setExpanded(true);
        }
        tree.select(jItem);
    }

    // Shell Location
    Monitor primary = display.getPrimaryMonitor();
    Rectangle bounds = primary.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}