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

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

Introduction

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

Prototype

public void setBackground(Color color) 

Source Link

Document

Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.

Usage

From source file:PopupMenuComposite.java

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

    shell.setLayout(new GridLayout(2, true));

    final Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

    final Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    Menu menu = new Menu(composite1);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Popup menu");

    menuItem.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            System.out.println("Menu item is disposed.");
        }/*from w  w  w  .  ja  v a2s.  co  m*/
    });

    menuItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Dispsoing ...");
            composite2.setMenu(null);
            composite2.dispose();
        }
    });

    composite1.setMenu(menu);
    composite2.setMenu(menu);

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:ControlEditorWithDialog.java

public static void main(String[] args) {

    final Shell shell = new Shell(display);
    shell.setText("Control Editor Two");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setBackground(color);
    composite.setBounds(0, 0, 300, 100);

    ControlEditor editor = new ControlEditor(composite);

    // Create the control associated with the editor
    Button button = new Button(composite, SWT.PUSH);
    button.setText("Change Color...");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            ColorDialog dialog = new ColorDialog(shell);
            if (color != null)
                dialog.setRGB(color.getRGB());
            RGB rgb = dialog.open();/*ww  w .  ja  v  a 2 s .com*/
            if (rgb != null) {
                if (color != null)
                    color.dispose();
                color = new Color(shell.getDisplay(), rgb);
                composite.setBackground(color);
            }
        }
    });
    // Place the editor along the bottom of the parent composite
    editor.grabHorizontal = true;
    editor.verticalAlignment = SWT.BOTTOM;
    Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumHeight = size.y;
    editor.setEditor(button);

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

From source file:ControlEditorSample.java

public static void main(String[] args) {

    final Shell shell = new Shell(display);
    shell.setText("Control Editor");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setBackground(color);
    composite.setBounds(0, 0, 300, 100);

    ControlEditor editor = new ControlEditor(composite);
    final Text text = new Text(composite, SWT.BORDER);
    text.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent event) {
            RGB rgb = (RGB) COLORS.get(text.getText());
            if (rgb != null) {
                if (color != null)
                    color.dispose();/*  w  w  w  .  j ava2 s .  co  m*/
                color = new Color(shell.getDisplay(), rgb);
                composite.setBackground(color);
            }
        }
    });

    // Place the editor in the top middle of the parent composite
    editor.horizontalAlignment = SWT.CENTER;
    editor.verticalAlignment = SWT.TOP;
    Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumWidth = size.x;
    editor.minimumHeight = size.y;
    editor.setEditor(text);

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

}

From source file:ControlListenerAdd.java

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

    shell.setLayout(new RowLayout());

    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout());
    composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    composite.addControlListener(new ControlListener() {
        public void controlMoved(ControlEvent e) {
        }/*from  w w w  .java 2  s  .co m*/

        public void controlResized(ControlEvent e) {
            System.out.println("Composite resize.");
        }
    });

    Button buttonAdd = new Button(shell, SWT.PUSH);
    buttonAdd.setText("Add new button");
    buttonAdd.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Button button = new Button(composite, SWT.PUSH);
            button.setText("Button #" + (count++));
            composite.layout(true);
            composite.pack();
        }
    });

    shell.setSize(450, 100);
    shell.open();

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

From source file:FocusTraversal.java

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

    shell.setLayout(new RowLayout());

    Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setLayout(new RowLayout());
    composite1.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

    Button button1 = new Button(composite1, SWT.PUSH);
    button1.setText("Button1");

    Button button3 = new Button(composite1, SWT.PUSH);
    button3.setText("Button3");

    Button radioButton1 = new Button(composite1, SWT.RADIO);
    radioButton1.setText("radio-1");
    Button radioButton2 = new Button(composite1, SWT.RADIO);
    radioButton2.setText("radio-2");

    Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setLayout(new RowLayout());
    composite2.setBackground(display.getSystemColor(SWT.COLOR_GREEN));

    Button button2 = new Button(composite2, SWT.PUSH);
    button2.setText("Button2");

    Combo combo = new Combo(composite2, SWT.DROP_DOWN);
    combo.add("combo");
    combo.select(0);/*  w w w. j a  v  a2s.com*/

    composite1.setTabList(new Control[] { button1, button3 });
    composite2.setTabList(new Control[] { button2, combo });

    shell.setTabList(new Control[] { composite2, composite1 });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Color black = display.getSystemColor(SWT.COLOR_BLACK);
    Shell shell = new Shell(display);
    shell.setText("Snippet 111");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 16; i++) {
        TreeItem itemI = new TreeItem(tree, SWT.NONE);
        itemI.setText("Item " + i);
        for (int j = 0; j < 16; j++) {
            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
            itemJ.setText("Item " + j);
        }//from  w  w  w  .ja va  2s  .c  o m
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, event -> {
        final TreeItem item = (TreeItem) event.item;
        if (item != null && item == lastItem[0]) {
            boolean showBorder = true;
            final Composite composite = new Composite(tree, SWT.NONE);
            if (showBorder)
                composite.setBackground(black);
            final Text text = new Text(composite, SWT.NONE);
            final int inset = showBorder ? 1 : 0;
            composite.addListener(SWT.Resize, e1 -> {
                Rectangle rect1 = composite.getClientArea();
                text.setBounds(rect1.x + inset, rect1.y + inset, rect1.width - inset * 2,
                        rect1.height - inset * 2);
            });
            Listener textListener = e2 -> {
                switch (e2.type) {
                case SWT.FocusOut:
                    item.setText(text.getText());
                    composite.dispose();
                    break;
                case SWT.Verify:
                    String newText = text.getText();
                    String leftText = newText.substring(0, e2.start);
                    String rightText = newText.substring(e2.end, newText.length());
                    GC gc = new GC(text);
                    Point size = gc.textExtent(leftText + e2.text + rightText);
                    gc.dispose();
                    size = text.computeSize(size.x, SWT.DEFAULT);
                    editor.horizontalAlignment = SWT.LEFT;
                    Rectangle itemRect = item.getBounds(), rect2 = tree.getClientArea();
                    editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                    int left = itemRect.x, right = rect2.x + rect2.width;
                    editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                    editor.minimumHeight = size.y + inset * 2;
                    editor.layout();
                    break;
                case SWT.Traverse:
                    switch (e2.detail) {
                    case SWT.TRAVERSE_RETURN:
                        item.setText(text.getText());
                        //FALL THROUGH
                    case SWT.TRAVERSE_ESCAPE:
                        composite.dispose();
                        e2.doit = false;
                    }
                    break;
                }
            };
            text.addListener(SWT.FocusOut, textListener);
            text.addListener(SWT.Traverse, textListener);
            text.addListener(SWT.Verify, textListener);
            editor.setEditor(composite, item);
            text.setText(item.getText());
            text.selectAll();
            text.setFocus();
        }
        lastItem[0] = item;
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeCellEditor.java

public static void main(String[] args) {
    final Display display = new Display();
    final Color black = display.getSystemColor(SWT.COLOR_BLACK);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 16; i++) {
        TreeItem itemI = new TreeItem(tree, SWT.NONE);
        itemI.setText("Item " + i);
        for (int j = 0; j < 16; j++) {
            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
            itemJ.setText("Item " + j);
        }//from www  .  j  a  va  2  s  .c  o m
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            final TreeItem item = (TreeItem) event.item;
            if (item != null && item == lastItem[0]) {
                boolean isCarbon = SWT.getPlatform().equals("carbon");
                final Composite composite = new Composite(tree, SWT.NONE);
                if (!isCarbon)
                    composite.setBackground(black);
                final Text text = new Text(composite, SWT.NONE);
                final int inset = isCarbon ? 0 : 1;
                composite.addListener(SWT.Resize, new Listener() {
                    public void handleEvent(Event e) {
                        Rectangle rect = composite.getClientArea();
                        text.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2,
                                rect.height - inset * 2);
                    }
                });
                Listener textListener = new Listener() {
                    public void handleEvent(final Event e) {
                        switch (e.type) {
                        case SWT.FocusOut:
                            item.setText(text.getText());
                            composite.dispose();
                            break;
                        case SWT.Verify:
                            String newText = text.getText();
                            String leftText = newText.substring(0, e.start);
                            String rightText = newText.substring(e.end, newText.length());
                            GC gc = new GC(text);
                            Point size = gc.textExtent(leftText + e.text + rightText);
                            gc.dispose();
                            size = text.computeSize(size.x, SWT.DEFAULT);
                            editor.horizontalAlignment = SWT.LEFT;
                            Rectangle itemRect = item.getBounds(), rect = tree.getClientArea();
                            editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                            int left = itemRect.x, right = rect.x + rect.width;
                            editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                            editor.minimumHeight = size.y + inset * 2;
                            editor.layout();
                            break;
                        case SWT.Traverse:
                            switch (e.detail) {
                            case SWT.TRAVERSE_RETURN:
                                item.setText(text.getText());
                                // FALL THROUGH
                            case SWT.TRAVERSE_ESCAPE:
                                composite.dispose();
                                e.doit = false;
                            }
                            break;
                        }
                    }
                };
                text.addListener(SWT.FocusOut, textListener);
                text.addListener(SWT.Traverse, textListener);
                text.addListener(SWT.Verify, textListener);
                editor.setEditor(composite, item);
                text.setText(item.getText());
                text.selectAll();
                text.setFocus();
            }
            lastItem[0] = item;
        }
    });
    shell.pack();
    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 av a2 s .  co m
    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:TableColumnFirstFix.java

public static void main(String[] args) {
    int rowCount = 40;
    int columnCount = 15;
    final Display display = new Display();
    Shell shell = new Shell(display);
    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  .  com*/
    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, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setSelection(leftTable.getSelectionIndices());
        }
    });
    rightTable.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event 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 teh selection background here.
    // This part only works on version 3.2 or later.
    Listener eraseListener = new Listener() {
        public void handleEvent(Event event) {
            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, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setTopIndex(leftTable.getTopIndex());
        }
    });
    ScrollBar vBarRight = rightTable.getVerticalBar();
    vBarRight.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event 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:org.talend.dataprofiler.chart.util.ChartUtils.java

/**
 * Create a AWT_SWT bridge composite for displaying the <CODE>ChartPanel</CODE>.
 * /*www  .  j a va2 s.  co  m*/
 * @param composite
 * @param gd
 * @param chartPanel
 */
public static ChartPanel createAWTSWTComp(Composite composite, GridData gd, JFreeChart chart) {

    ChartPanel chartPanel = new ChartPanel(chart);
    Composite frameComp = new Composite(composite, SWT.EMBEDDED);
    frameComp.setLayout(new GridLayout());
    frameComp.setLayoutData(gd);
    frameComp.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));

    Frame frame = SWT_AWT.new_Frame(frameComp);
    frame.setLayout(new java.awt.GridLayout());
    frame.add(chartPanel);
    frame.validate();

    return chartPanel;
}