Example usage for org.eclipse.swt.widgets Button pack

List of usage examples for org.eclipse.swt.widgets Button pack

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button pack.

Prototype

public void pack() 

Source Link

Document

Causes the receiver to be resized to its preferred size.

Usage

From source file:ScreenshotCaptureGC.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();
    button.setLocation(10, 140);/*from   w w w.jav a 2 s .c  om*/
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            GC gc = new GC(display);
            final Image image = new Image(display, 400, 400);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, 400 + 10, 400 + 10);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.pack();
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 249");
    Rectangle clientArea = shell.getClientArea();
    shell.setBounds(clientArea.x + 10, clientArea.y + 10, 300, 200);
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(clientArea.x + 100, clientArea.y + 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);/*  w w w .  j a  v  a 2  s  .  co m*/

    // create the first page's content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();

    // create the second page's content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();

    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(clientArea.x + 10, clientArea.y + 10, 80, 25);
    pageButton.addListener(SWT.Selection, event -> {
        pageNum = ++pageNum % 2;
        layout.topControl = pageNum == 0 ? page0 : page1;
        contentPanel.layout();
    });

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

From source file:StackLayoutSwitchComposites.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 300, 200);/*from ww w  . j  a v a  2  s  .  c o m*/
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(100, 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);

    // create the first page's content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();

    // create the second page's content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();

    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(10, 10, 80, 25);
    pageButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            pageNum = ++pageNum % 2;
            layout.topControl = pageNum == 0 ? page0 : page1;
            contentPanel.layout();
        }
    });

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

From source file:CaptureWidgetImageGC.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);/*from   w w w  .  j ava2  s.c  o  m*/
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
        new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();
    button.setLocation(10, 140);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            Point tableSize = table.getSize();
            GC gc = new GC(table);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            Shell popup = new Shell(shell);
            popup.setText("Image");
            popup.addListener(SWT.Close, new Listener() {
                public void handleEvent(Event e) {
                    image.dispose();
                }
            });

            Canvas canvas = new Canvas(popup, SWT.NONE);
            canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10);
            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(image, 0, 0);
                }
            });
            popup.pack();
            popup.open();
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
            new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
        GC gc = new GC(images[i]);
        gc.setBackground(display.getSystemColor(colors[i]));
        gc.fillRectangle(images[i].getBounds());
        gc.dispose();/*from   w ww  .  j av a 2s.co  m*/
    }

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
        Rectangle bounds = images[i].getBounds();
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
        layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);

    shell.addListener(SWT.Paint, event -> {
        GC gc = event.gc;
        Point margin = new Point(10, 10);
        layout.setWidth(shell.getClientArea().width - 2 * margin.x);
        layout.draw(event.gc, margin.x, margin.y);
        for (int i = 0; i < images.length; i++) {
            int offset = imageOffsets[i];
            int lineIndex1 = layout.getLineIndex(offset);
            FontMetrics lineMetrics1 = layout.getLineMetrics(lineIndex1);
            Point point1 = layout.getLocation(offset, false);
            GlyphMetrics glyphMetrics1 = layout.getStyle(offset).metrics;
            gc.drawImage(images[i], point1.x + margin.x,
                    point1.y + margin.y + lineMetrics1.getAscent() - glyphMetrics1.ascent);
        }
        int lineIndex2 = layout.getLineIndex(buttonOffset);
        FontMetrics lineMetrics2 = layout.getLineMetrics(lineIndex2);
        Point point2 = layout.getLocation(buttonOffset, false);
        GlyphMetrics glyphMetrics2 = layout.getStyle(buttonOffset).metrics;
        button.setLocation(point2.x + margin.x,
                point2.y + margin.y + lineMetrics2.getAscent() - glyphMetrics2.ascent);
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
        images[i].dispose();
    }
    display.dispose();
}

From source file:TextLayoutImageControl.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
            new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
        GC gc = new GC(images[i]);
        gc.setBackground(display.getSystemColor(colors[i]));
        gc.fillRectangle(images[i].getBounds());
        gc.dispose();/*from www . j  a v  a2  s.  c o m*/
    }

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
        Rectangle bounds = images[i].getBounds();
        TextStyle imageStyle = new TextStyle(null, null, null);
        imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
        layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);

    shell.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event event) {
            GC gc = event.gc;
            Point margin = new Point(10, 10);
            layout.setWidth(shell.getClientArea().width - 2 * margin.x);
            layout.draw(event.gc, margin.x, margin.y);
            for (int i = 0; i < images.length; i++) {
                int offset = imageOffsets[i];
                int lineIndex = layout.getLineIndex(offset);
                FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
                Point point = layout.getLocation(offset, false);
                GlyphMetrics glyphMetrics = layout.getStyle(offset).metrics;
                gc.drawImage(images[i], point.x + margin.x,
                        point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent);
            }
            int lineIndex = layout.getLineIndex(buttonOffset);
            FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
            Point point = layout.getLocation(buttonOffset, false);
            GlyphMetrics glyphMetrics = layout.getStyle(buttonOffset).metrics;
            button.setLocation(point.x + margin.x,
                    point.y + margin.y + lineMetrics.getAscent() - glyphMetrics.ascent);
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
        images[i].dispose();
    }
    display.dispose();
}

From source file:Snippet134.java

public static void main(String[] args) {
    final Display display = new Display();
    // Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    // define a region that looks like a key hole
    Region region = new Region();
    region.add(circle(67, 67, 67));/*from w w  w . j  av a  2s. c  o  m*/
    region.subtract(circle(20, 67, 50));
    region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
    // define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    // add ability to move shell around
    Listener l = new Listener() {
        Point origin;

        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
                origin = new Point(e.x, e.y);
                break;
            case SWT.MouseUp:
                origin = null;
                break;
            case SWT.MouseMove:
                if (origin != null) {
                    Point p = display.map(shell, null, e.x, e.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
                break;
            }
        }
    };
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseUp, l);
    shell.addListener(SWT.MouseMove, l);
    // add ability to close shell
    Button b = new Button(shell, SWT.PUSH);
    b.setBackground(shell.getBackground());
    b.setText("close");
    b.pack();
    b.setLocation(10, 68);
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            shell.close();
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    region.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    //Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setText("Snippet 134");
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    //define a region that looks like a key hole
    Region region = new Region();
    region.add(circle(67, 67, 67));/* ww w  .  ja  v  a2  s  .  c om*/
    region.subtract(circle(20, 67, 50));
    region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
    //define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    //add ability to move shell around
    Listener l = new Listener() {
        /** The x/y of the MouseDown, relative to top-left of the shell. */
        Point origin;

        @Override
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
                Point point = shell.toDisplay(e.x, e.y);
                Point loc = shell.getLocation();
                origin = new Point(point.x - loc.x, point.y - loc.y);
                break;
            case SWT.MouseUp:
                origin = null;
                break;
            case SWT.MouseMove:
                if (origin != null) {
                    Point p = display.map(shell, null, e.x, e.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
                break;
            }
        }
    };
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseUp, l);
    shell.addListener(SWT.MouseMove, l);
    //add ability to close shell
    Button b = new Button(shell, SWT.PUSH);
    b.setBackground(shell.getBackground());
    b.setText("close");
    b.pack();
    b.setLocation(10, 68);
    b.addListener(SWT.Selection, e -> shell.close());
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    region.dispose();
    display.dispose();
}

From source file:ToolTipBalloon.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Image image = null;/*from w  w  w.jav a 2 s.  c  o m*/
    final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
    tip.setMessage(
            "Here is a message for the user. When the message is too long it wraps. I should say something cool but nothing comes to my mind.");
    Tray tray = display.getSystemTray();
    if (tray != null) {
        TrayItem item = new TrayItem(tray, SWT.NONE);
        image = new Image(display, "yourFile.gif");
        item.setImage(image);
        tip.setText("Notification from a tray item");
        item.setToolTip(tip);
    } else {
        tip.setText("Notification from anywhere");
        tip.setLocation(400, 400);
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Press for balloon tip");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            tip.setVisible(true);
        }
    });
    button.pack();
    shell.setBounds(50, 50, 300, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (image != null)
        image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 225");
    Image image = null;//ww w . j  a va  2  s  . c o m
    final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
    tip.setMessage(
            "Here is a message for the user. When the message is too long it wraps. I should say something cool but nothing comes to my mind.");
    Tray tray = display.getSystemTray();
    if (tray != null) {
        TrayItem item = new TrayItem(tray, SWT.NONE);
        image = display.getSystemImage(SWT.ICON_INFORMATION);
        item.setImage(image);
        tip.setText("Notification from a tray item");
        item.setToolTip(tip);
    } else {
        tip.setText("Notification from anywhere");
        tip.setLocation(400, 400);
    }
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Press for balloon tip");
    button.addListener(SWT.Selection, event -> tip.setVisible(true));
    Rectangle clientArea = shell.getClientArea();
    button.setLocation(clientArea.x, clientArea.y);
    button.pack();
    shell.setBounds(50, 50, 300, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (image != null)
        image.dispose();
    display.dispose();
}