Example usage for org.eclipse.swt.widgets Composite Composite

List of usage examples for org.eclipse.swt.widgets Composite Composite

Introduction

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

Prototype

public Composite(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

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);//  ww w .j ava 2  s.c  om
    // 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:TableColumnResizeTableResize.java

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

    final Composite comp = new Composite(shell, SWT.NONE);
    final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
    table.setHeaderVisible(true);/*from   www .  ja v a 2 s.  co  m*/
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    for (int i = 0; i < 10; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "item 0" + i, "item 1" + i });
    }
    comp.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            Rectangle area = comp.getClientArea();
            Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            int width = area.width - 2 * table.getBorderWidth();
            if (preferredSize.y > area.height + table.getHeaderHeight()) {
                // Subtract the scrollbar width from the total column width
                // if a vertical scrollbar will be required
                Point vBarSize = table.getVerticalBar().getSize();
                width -= vBarSize.x;
            }
            Point oldSize = table.getSize();
            if (oldSize.x > area.width) {
                // table is getting smaller so make the columns
                // smaller first and then resize the table to
                // match the client area width
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
                table.setSize(area.width, area.height);
            } else {
                // table is getting bigger so make the table
                // bigger first and then make the columns wider
                // to match the client area width
                table.setSize(area.width, area.height);
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
            }
        }
    });

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

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

public static void main(String[] args) {
    int rowCount = 40;
    int columnCount = 15;
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 234");
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.BORDER);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
    parent.setLayout(layout);// w  ww .  j a  va 2 s . c  om
    final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
    leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
    leftTable.setHeaderVisible(true);
    final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    rightTable.setHeaderVisible(true);
    GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
    rightTable.setLayoutData(table2Data);
    // Create columns
    TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
    column1.setText("Name");
    column1.setWidth(150);
    for (int i = 0; i < columnCount; i++) {
        TableColumn column = new TableColumn(rightTable, SWT.NONE);
        column.setText("Value " + i);
        column.setWidth(200);
    }
    // Create rows
    for (int i = 0; i < rowCount; i++) {
        TableItem item = new TableItem(leftTable, SWT.NONE);
        item.setText("item " + i);
        item = new TableItem(rightTable, SWT.NONE);
        for (int j = 0; j < columnCount; j++) {
            item.setText(j, "Item " + i + " value @ " + j);
        }
    }
    // Make selection the same in both tables
    leftTable.addListener(SWT.Selection, event -> rightTable.setSelection(leftTable.getSelectionIndices()));
    rightTable.addListener(SWT.Selection, event -> leftTable.setSelection(rightTable.getSelectionIndices()));
    // On Windows, the selection is gray if the table does not have focus.
    // To make both tables appear in focus, draw the selection background here.
    // This part only works on version 3.2 or later.
    Listener eraseListener = event -> {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) != 0) {
            GC gc = event.gc;
            Rectangle rect = event.getBounds();
            gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
            gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
            gc.fillRectangle(rect);
            event.detail &= ~SWT.SELECTED;
        }
    };

    leftTable.addListener(SWT.EraseItem, eraseListener);
    rightTable.addListener(SWT.EraseItem, eraseListener);
    // Make vertical scrollbars scroll together
    ScrollBar vBarLeft = leftTable.getVerticalBar();
    vBarLeft.addListener(SWT.Selection, event -> rightTable.setTopIndex(leftTable.getTopIndex()));
    ScrollBar vBarRight = rightTable.getVerticalBar();
    vBarRight.addListener(SWT.Selection, event -> leftTable.setTopIndex(rightTable.getTopIndex()));
    // Horizontal bar on second table takes up a little extra space.
    // To keep vertical scroll bars in sink, force table1 to end above
    // horizontal scrollbar
    ScrollBar hBarRight = rightTable.getHorizontalBar();
    Label spacer = new Label(parent, SWT.NONE);
    GridData spacerData = new GridData();
    spacerData.heightHint = hBarRight.getSize().y;
    spacer.setVisible(false);
    parent.setBackground(leftTable.getBackground());

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

From source file:JavascriptExec.java

public static void main(String[] args) {
    final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Browser browser = new Browser(shell, SWT.BORDER);
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout(SWT.VERTICAL));
    final Text text = new Text(comp, SWT.MULTI);
    text.setText("var newNode = document.createElement('P'); \r\n"
            + "var text = document.createTextNode('At least when I am around');\r\n"
            + "newNode.appendChild(text);\r\n" + "document.getElementById('myid').appendChild(newNode);\r\n"
            + "\r\n" + "document.bgColor='yellow';");
    final Button button = new Button(comp, SWT.PUSH);
    button.setText("Execute Script");
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            boolean result = browser.execute(text.getText());
            if (!result) {
                /* Script may fail or may not be supported on certain platforms. */
                System.out.println("Script was not executed.");
            }//from w ww  .  ja  va  2 s.c  o m
        }
    });
    browser.setText(html);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setText("Snippet 166");
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for (int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0)
            label.setImage(image1);/*from w  ww.ja  v a 2s  . co m*/
        if (i % 3 == 1)
            label.setImage(image2);
        if (i % 3 == 2)
            label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;
    parent.setLayout(layout);

    scrollComposite.setContent(parent);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.addControlListener(ControlListener.controlResizedAdapter(e -> {
        Rectangle r = scrollComposite.getClientArea();
        scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
    }));

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

From source file:CompositeCreateDisposeChildren.java

public static void main(String args[]) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Push");
    pageComposite = new Composite(shell, SWT.NONE);
    pageComposite.setLayout(new GridLayout());
    pageComposite.setLayoutData(new GridData());

    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if ((pageComposite != null) && (!pageComposite.isDisposed())) {
                pageComposite.dispose();
            }/*from  ww  w. j  a v a2s .  c o m*/
            pageComposite = new Composite(shell, SWT.NONE);
            pageComposite.setLayout(new GridLayout());
            pageComposite.setLayoutData(new GridData());
            if (pageNum++ % 2 == 0) {
                Table table = new Table(pageComposite, SWT.BORDER);
                table.setLayoutData(new GridData());
                for (int i = 0; i < 5; i++) {
                    new TableItem(table, SWT.NONE).setText("table item " + i);
                }
            } else {
                new Button(pageComposite, SWT.RADIO).setText("radio");
            }
            shell.layout(true);
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing DND Example");
    GridLayout layout = new GridLayout(1, false);
    shell.setLayout(layout);//from  www.  j av a 2  s .c o m

    Text swtText = new Text(shell, SWT.BORDER);
    swtText.setText("SWT Text");
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    swtText.setLayoutData(data);
    setDragDrop(swtText);

    Composite comp = new Composite(shell, SWT.EMBEDDED);
    java.awt.Frame frame = SWT_AWT.new_Frame(comp);
    JTextField swingText = new JTextField(40);
    swingText.setText("Swing Text");
    swingText.setDragEnabled(true);
    frame.add(swingText);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.heightHint = swingText.getPreferredSize().height;
    comp.setLayoutData(data);

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

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

public static void main(String[] args) {
    final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 161");
    shell.setLayout(new FillLayout());
    final Browser browser;
    try {/*from www  . j a v  a  2  s .  c  o  m*/
        browser = new Browser(shell, SWT.BORDER);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout(SWT.VERTICAL));
    final Text text = new Text(comp, SWT.MULTI);
    text.setText("var newNode = document.createElement('P'); \r\n"
            + "var text = document.createTextNode('At least when I am around');\r\n"
            + "newNode.appendChild(text);\r\n" + "document.getElementById('myid').appendChild(newNode);\r\n"
            + "\r\n" + "document.bgColor='yellow';");
    final Button button = new Button(comp, SWT.PUSH);
    button.setText("Execute Script");
    button.addListener(SWT.Selection, event -> {
        boolean result = browser.execute(text.getText());
        if (!result) {
            /* Script may fail or may not be supported on certain platforms. */
            System.out.println("Script was not executed.");
        }
    });
    browser.setText(html);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] arg) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Skin example");
    shell.setLayout(new GridLayout());

    Group container = new Group(shell, SWT.None);
    container.setText("Container");
    container.setLayout(new GridLayout(3, false));

    Composite child1 = new Composite(container, SWT.BORDER);
    child1.setLayout(new GridLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");

    Composite child2 = new Composite(container, SWT.BORDER);
    child2.setLayout(new GridLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");

    final Composite child3 = new Composite(container, SWT.BORDER);
    child3.setLayout(new GridLayout());
    new Text(child3, SWT.BORDER).setText("Text in pane3");

    display.addListener(SWT.Skin, event -> {
        System.out.println("Skin: " + event.widget);
        setBackground(event.display, (Control) event.widget);
    });//from w w  w  .  ja va2 s  . c  om

    Composite buttonContainer = new Composite(shell, SWT.NONE);
    buttonContainer.setLayout(new GridLayout(3, false));
    Button reskin = new Button(buttonContainer, SWT.PUSH);
    reskin.setText("Reskin All");
    reskin.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.ALL);
    }));
    Button reskin2 = new Button(buttonContainer, SWT.PUSH);
    reskin2.setText("Reskin Shell");
    reskin2.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        shell.reskin(SWT.None);
    }));
    Button reskin3 = new Button(buttonContainer, SWT.PUSH);
    reskin3.setText("Reskin Right Composite");
    reskin3.addSelectionListener(widgetSelectedAdapter(e -> {
        System.out.println("=======");
        child3.reskin(SWT.ALL);
    }));

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setText("Snippet 282");
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;//w ww  .  j a  va2s.  c o  m
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(SWT.Selection, event -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(SWT.Selection, event -> {
        Image image = imageButton.getImage();
        if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(new Object[] { image.getImageData(), imageText.getText() },
                    new Transfer[] { imageTransfer, textTransfer });
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(SWT.Selection, event -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null)
                image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
        } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
        }
        String text = (String) clipboard.getContents(TextTransfer.getInstance());
        if (text != null) {
            imageText.setText(text);
        } else {
            imageText.setText("");
        }
    });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        imageButton.setText("");
        Image image = imageButton.getImage();
        if (image != null)
            image.dispose();
        imageButton.setImage(null);
        imageText.setText("");
    });

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