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

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

Introduction

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

Prototype

public void setLayoutData(Object layoutData) 

Source Link

Document

Sets the layout data associated with the receiver to the argument.

Usage

From source file:Snippet177.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.wrap = true;//from   w w  w.j  a  v  a 2s . co m
    layout.fill = true;
    layout.justify = false;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

From source file:DialogOKCancelFormLayout.java

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

    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");

    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);//from  w ww. ja  v  a  2  s. com
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);

    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 71");
    shell.pack();//from   w  ww .  jav a2 s  .  com
    shell.open();
    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");

    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);

    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();

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

From source file:RowLayoutAlignWidgets.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;/*from   w  ww .  j a  v  a  2 s.co  m*/
    layout.fill = false;
    layout.justify = true;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

From source file:RowLayoutAlignColumn.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;/*from w  w w  . java2 s.  co  m*/
    layout.fill = true;
    layout.justify = false;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 176");
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;/*from www.  j  a  v a  2  s .  c o  m*/
    layout.fill = false;
    layout.justify = true;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 177");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.wrap = true;/*from   ww  w.j  a v  a2 s . c  o m*/
    layout.fill = true;
    layout.justify = false;
    shell.setLayout(layout);

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 1");
    b = new Button(shell, SWT.PUSH);

    b.setText("Button 2");

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");

    b = new Button(shell, SWT.PUSH);
    b.setText("Not shown");
    b.setVisible(false);
    RowData data = new RowData();
    data.exclude = true;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 high");
    data = new RowData();
    data.height = 200;
    b.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 200 wide");
    data = new RowData();
    data.width = 200;
    b.setLayoutData(data);

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

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

public static void main(String[] arg) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 334");
    shell.setLayout(new GridLayout(2, false));
    Label label = new Label(shell, SWT.NONE);
    label.setText("Test:");
    canvas = new Canvas(shell, SWT.MULTI | SWT.BORDER);
    final Caret caret = new Caret(canvas, SWT.NONE);
    canvas.addPaintListener(e -> {//from ww  w.  j a v a 2s . co  m
        GC gc = e.gc;
        gc.drawText(text, 10, 10);
        caret.setBounds(10, 10, 2, gc.getFontMetrics().getHeight());
        Rectangle rect = canvas.getClientArea();
        if (canvas.isFocusControl()) {
            gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
        }
    });
    canvas.addTraverseListener(e -> {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
            e.doit = true; // enable traversal
            break;
        }
    });

    // key listener enables traversal out)
    canvas.addKeyListener(keyPressedAdapter(e -> {
    }));

    canvas.addFocusListener(focusGainedAdapter(event -> canvas.redraw()));
    canvas.addFocusListener(focusLostAdapter(event -> canvas.redraw()));
    canvas.addMouseListener(mouseDownAdapter(e -> canvas.setFocus()));
    Accessible acc = canvas.getAccessible();
    acc.addRelation(ACC.RELATION_LABELLED_BY, label.getAccessible());
    acc.addAccessibleControlListener(new AccessibleControlAdapter() {
        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_TEXT;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle rect = canvas.getBounds();
            Point pt = shell.toDisplay(rect.x, rect.y);
            e.x = pt.x;
            e.y = pt.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getValue(AccessibleControlEvent e) {
            e.result = text;
        }

        @Override
        public void getFocus(AccessibleControlEvent e) {
            e.childID = ACC.CHILDID_SELF;
        }

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = 0;
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            e.detail = ACC.STATE_NORMAL | ACC.STATE_FOCUSABLE;
            if (canvas.isFocusControl())
                e.detail |= ACC.STATE_FOCUSED | ACC.STATE_SELECTABLE;
        }
    });
    acc.addAccessibleTextListener(new AccessibleTextExtendedAdapter() {
        @Override
        public void getSelectionRange(AccessibleTextEvent e) {
            // select the first 4 characters for testing
            e.offset = 0;
            e.length = 4;
        }

        @Override
        public void getCaretOffset(AccessibleTextEvent e) {
            e.offset = 0;
        }

        @Override
        public void getTextBounds(AccessibleTextEvent e) {
            // for now, assume that start = 0 and end = text.length
            GC gc = new GC(canvas);
            Point extent = gc.textExtent(text);
            gc.dispose();
            Rectangle rect = display.map(canvas, null, 10, 10, extent.x, extent.y);
            e.x = rect.x;
            e.y = rect.y;
            e.width = rect.width;
            e.height = rect.height;
        }

        @Override
        public void getText(AccessibleTextEvent e) {
            int start = 0, end = text.length();
            switch (e.type) {
            case ACC.TEXT_BOUNDARY_ALL:
                start = e.start;
                end = e.end;
                break;
            case ACC.TEXT_BOUNDARY_CHAR:
                start = e.count >= 0 ? e.start + e.count : e.start - e.count;
                start = Math.max(0, Math.min(end, start));
                end = start;
                e.count = start - e.start;
                e.start = start;
                e.end = start;
                break;
            case ACC.TEXT_BOUNDARY_LINE:
                int offset = e.count <= 0 ? e.start : e.end;
                offset = Math.min(offset, text.length());
                int lineCount = 0;
                int index = 0;
                while (index != -1) {
                    lineCount++;
                    index = text.indexOf("\n", index);
                    if (index != -1)
                        index++;
                }
                e.count = e.count < 0 ? Math.max(e.count, -lineCount) : Math.min(e.count, lineCount);
                index = 0;
                int lastIndex = 0;
                String[] lines = new String[lineCount];
                for (int i = 0; i < lines.length; i++) {
                    index = text.indexOf("\n", index);
                    lines[i] = index != -1 ? text.substring(lastIndex, index) : text.substring(lastIndex);
                    lastIndex = index;
                    index++;
                }
                int len = 0;
                int lineAtOffset = 0;
                for (int i = 0; i < lines.length; i++) {
                    len += lines[i].length();
                    if (len >= e.offset) {
                        lineAtOffset = i;
                        break;
                    }
                }
                int result = Math.max(0, Math.min(lineCount - 1, lineAtOffset + e.count));
                e.count = result - lineAtOffset;
                e.result = lines[result];
                break;
            }
            e.result = text.substring(start, end);
        }

        @Override
        public void getSelectionCount(AccessibleTextEvent e) {
            e.count = 1;
        }

        @Override
        public void getSelection(AccessibleTextEvent e) {
            // there is only 1 selection, so index = 0
            getSelectionRange(e);
            e.start = e.offset;
            e.end = e.offset + e.length;
        }

        @Override
        public void getRanges(AccessibleTextEvent e) {
            // for now, ignore bounding box
            e.start = 0;
            e.end = text.length() - 1;
        }

        @Override
        public void getCharacterCount(AccessibleTextEvent e) {
            e.count = text.length();
        }

        @Override
        public void getVisibleRanges(AccessibleTextEvent e) {
            e.start = 0;
            e.end = text.length() - 1;
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:FocusListenerUsing.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));
    shell.setText("One Potato, Two Potato");
    // Create the focus listener
    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("I'm It!");
        }//from   w w  w . j ava  2  s  .  c om

        public void focusLost(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("Pick Me!");
        }
    };

    // Create the buttons and add the listener to each one
    for (int i = 0; i < 6; i++) {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        button.setText("Pick Me!");
        button.addFocusListener(listener);
    }

    // Display the window
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:FocusListenerExample.java

/**
 * The application entry point//from w  w w .  jav a 2 s .c o m
 * 
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // Create the shell
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));
    shell.setText("One Potato, Two Potato");

    // Create the focus listener
    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("I'm It!");
        }

        public void focusLost(FocusEvent event) {
            Button button = (Button) event.getSource();
            button.setText("Pick Me!");
        }
    };

    // Create the buttons and add the listener to each one
    for (int i = 0; i < 6; i++) {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        button.setText("Pick Me!");
        button.addFocusListener(listener);
    }

    // Display the window
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}