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

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

Introduction

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

Prototype

public Tree(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:org.eclipse.swt.snippets.Snippet226.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);//from  www . java  2s  . c  o m
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    tree.addListener(SWT.EraseItem, event -> {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) != 0) {
            GC gc = event.gc;
            Rectangle area = tree.getClientArea();
            /*
             * If you wish to paint the selection beyond the end of
             * last column, you must change the clipping region.
             */
            int columnCount1 = tree.getColumnCount();
            if (event.index == columnCount1 - 1 || columnCount1 == 0) {
                int width = area.x + area.width - event.x;
                if (width > 0) {
                    Region region = new Region();
                    gc.getClipping(region);
                    region.add(event.x, event.y, width, event.height);
                    gc.setClipping(region);
                    region.dispose();
                }
            }
            gc.setAdvanced(true);
            if (gc.getAdvanced())
                gc.setAlpha(127);
            Rectangle rect = event.getBounds();
            Color foreground = gc.getForeground();
            Color background = gc.getBackground();
            gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
            gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
            gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
            // restore colors for subsequent drawing
            gc.setForeground(foreground);
            gc.setBackground(background);
            event.detail &= ~SWT.SELECTED;
        }
    });
    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TreeItem");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*from   ww  w  .j a  v a 2s.com*/
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
        column.setWidth(100);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    Listener paintListener = new Listener() {
        @Override
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                event.width = size.x;
                event.height = Math.max(event.height, size.y);
                break;
            }
            case SWT.PaintItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
                event.gc.drawText(text, event.x, event.y + offset2, true);
                break;
            }
            case SWT.EraseItem: {
                event.detail &= ~SWT.FOREGROUND;
                break;
            }
            }
        }

        String getText(TreeItem item, int column) {
            String text = item.getText(column);
            if (column != 0) {
                TreeItem parent = item.getParentItem();
                int index = parent == null ? tree.indexOf(item) : parent.indexOf(item);
                if ((index + column) % 3 == 1) {
                    text += "\nnew line";
                }
                if ((index + column) % 3 == 2) {
                    text += "\nnew line\nnew line";
                }
            }
            return text;
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);
    tree.addListener(SWT.EraseItem, paintListener);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 266");
    shell.setLayout(new GridLayout(2, true));

    TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
    tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

    TabItem item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Widget");
    Composite composite = new Composite(tabFolder, SWT.NONE);
    composite.setLayout(new GridLayout());
    Tree tree = new Tree(composite, SWT.BORDER);
    item.setControl(composite);//from   ww  w  . j a  va 2 s . co  m
    tree.setHeaderVisible(true);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Standard");
    TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Widget");
    TreeItem branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Efficient", "Portable" });
    TreeItem leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch.setExpanded(true);
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Native", "Controls" });
    leaf = new TreeItem(branch, SWT.NONE);
    leaf.setText(new String[] { "Cross", "Platform" });
    branch = new TreeItem(tree, SWT.NONE);
    branch.setText(new String[] { "Cross", "Platform" });
    column1.pack();
    column2.pack();

    item = new TabItem(tabFolder, SWT.NONE);
    item.setText("Toolkit");

    Button button = new Button(shell, SWT.CHECK);
    button.setText("Totally");
    button.setSelection(true);
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

    button = new Button(shell, SWT.PUSH);
    button.setText("Awesome");
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

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

From source file:TreeItemLines.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TreeItem");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);//  w  w  w . j a  va  2 s. co  m
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
        column.setWidth(100);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                event.width = size.x;
                event.height = Math.max(event.height, size.y);
                break;
            }
            case SWT.PaintItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
                event.gc.drawText(text, event.x, event.y + offset2, true);
                break;
            }
            case SWT.EraseItem: {
                event.detail &= ~SWT.FOREGROUND;
                break;
            }
            }
        }

        String getText(TreeItem item, int column) {
            String text = item.getText(column);
            if (column != 0) {
                TreeItem parent = item.getParentItem();
                int index = parent == null ? tree.indexOf(item) : parent.indexOf(item);
                if ((index + column) % 3 == 1) {
                    text += "\nnew line";
                }
                if ((index + column) % 3 == 2) {
                    text += "\nnew line\nnew line";
                }
            }
            return text;
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);
    tree.addListener(SWT.EraseItem, paintListener);

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

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 296");
    shell.setBounds(10, 10, 300, 300);//  www .j  av  a  2  s  .  co m
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL);
    sc.setBounds(10, 10, 280, 200);
    final int clientWidth = sc.getClientArea().width;

    final Tree tree = new Tree(sc, SWT.NONE);
    for (int i = 0; i < 99; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        new TreeItem(item, SWT.NONE).setText("child");
    }
    sc.setContent(tree);
    int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    tree.setSize(clientWidth, prefHeight);
    /*
     * The following listener ensures that the Tree is always large
     * enough to not need to show its own vertical scrollbar.
     */
    tree.addTreeListener(new TreeListener() {
        @Override
        public void treeExpanded(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }

        @Override
        public void treeCollapsed(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }
    });
    /*
     * The following listener ensures that a newly-selected item
     * in the Tree is always visible.
     */
    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selectedItems = tree.getSelection();
        if (selectedItems.length > 0) {
            Rectangle itemRect = selectedItems[0].getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (itemRect.x < origin.x || itemRect.y < origin.y
                    || itemRect.x + itemRect.width > origin.x + area.width
                    || itemRect.y + itemRect.height > origin.y + area.height) {
                sc.setOrigin(itemRect.x, itemRect.y);
            }
        }
    }));
    /*
     * The following listener scrolls the Tree one item at a time
     * in response to MouseWheel events.
     */
    tree.addListener(SWT.MouseWheel, event -> {
        Point origin = sc.getOrigin();
        if (event.count < 0) {
            origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y);
        } else {
            origin.y = Math.max(origin.y - tree.getItemHeight(), 0);
        }
        sc.setOrigin(origin);
    });

    Button downButton = new Button(shell, SWT.PUSH);
    downButton.setBounds(10, 220, 120, 30);
    downButton.setText("Down 10px");
    downButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y + 10));
    Button upButton = new Button(shell, SWT.PUSH);
    upButton.setBounds(140, 220, 120, 30);
    upButton.setText("Up 10px");
    upButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y - 10));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeTableBarChart.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Show results as a bar chart in Tree");
    final Tree tree = new Tree(shell, SWT.BORDER);
    tree.setHeaderVisible(true);//from w w w . j a  v a2  s  .com
    tree.setLinesVisible(true);
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Bug Status");
    column1.setWidth(100);
    final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Percent");
    column2.setWidth(200);
    String[] states = new String[] { "Resolved", "New", "Won't Fix", "Invalid" };
    String[] teams = new String[] { "UI", "SWT", "OSGI" };
    for (int i = 0; i < teams.length; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(teams[i]);
        for (int j = 0; j < states.length; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText(states[j]);
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    tree.addListener(SWT.PaintItem, new Listener() {
        int[] percents = new int[] { 50, 30, 5, 15 };

        public void handleEvent(Event event) {
            if (event.index == 1) {
                TreeItem item = (TreeItem) event.item;
                TreeItem parent = item.getParentItem();
                if (parent != null) {
                    GC gc = event.gc;
                    int index = parent.indexOf(item);
                    int percent = percents[index];
                    Color foreground = gc.getForeground();
                    Color background = gc.getBackground();
                    gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                    gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
                    int width = (column2.getWidth() - 1) * percent / 100;
                    gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
                    Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
                    gc.drawRectangle(rect2);
                    gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
                    String text = percent + "%";
                    Point size = event.gc.textExtent(text);
                    int offset = Math.max(0, (event.height - size.y) / 2);
                    gc.drawText(text, event.x + 2, event.y + offset, true);
                    gc.setForeground(background);
                    gc.setBackground(foreground);
                }
            }
        }
    });

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 220");
    shell.setBounds(10, 10, 350, 200);/*from   ww w  . j  a  va 2 s.co  m*/
    Image xImage = new Image(display, 16, 16);
    GC gc = new GC(xImage);
    gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
    gc.drawLine(1, 1, 14, 14);
    gc.drawLine(1, 14, 14, 1);
    gc.drawOval(2, 2, 11, 11);
    gc.dispose();
    final int IMAGE_MARGIN = 2;
    final Tree tree = new Tree(shell, SWT.CHECK);
    tree.setBounds(10, 10, 300, 150);
    TreeItem item = new TreeItem(tree, SWT.NONE);
    item.setText("root item");
    for (int i = 0; i < 4; i++) {
        TreeItem newItem = new TreeItem(item, SWT.NONE);
        newItem.setText("descendent " + i);
        if (i % 2 == 0)
            newItem.setData(xImage);
        item.setExpanded(true);
        item = newItem;
    }

    /*
     * NOTE: MeasureItem and PaintItem are called repeatedly.  Therefore it is
     * critical for performance that these methods be as efficient as possible.
     */
    tree.addListener(SWT.MeasureItem, event -> {
        TreeItem item1 = (TreeItem) event.item;
        Image trailingImage = (Image) item1.getData();
        if (trailingImage != null) {
            event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
        }
    });
    tree.addListener(SWT.PaintItem, event -> {
        TreeItem item1 = (TreeItem) event.item;
        Image trailingImage = (Image) item1.getData();
        if (trailingImage != null) {
            int x = event.x + event.width + IMAGE_MARGIN;
            int itemHeight = tree.getItemHeight();
            int imageHeight = trailingImage.getBounds().height;
            int y = event.y + (itemHeight - imageHeight) / 2;
            event.gc.drawImage(trailingImage, x, y);
        }
    });

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

From source file:TreeEventMeasurePaintErase.java

public static void main(String[] args) {
    Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
    Shell shell = new Shell(display);
    shell.setText("Images on the right side of the TreeItem");
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*ww w .j  a  v a 2s . com*/
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }
    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    Listener paintListener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                Rectangle rect = image.getBounds();
                event.width += rect.width;
                event.height = Math.max(event.height, rect.height + 2);
                break;
            }
            case SWT.PaintItem: {
                int x = event.x + event.width;
                Rectangle rect = image.getBounds();
                int offset = Math.max(0, (event.height - rect.height) / 2);
                event.gc.drawImage(image, x, event.y + offset);
                break;
            }
            }
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);

    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (image != null)
        image.dispose();
    display.dispose();
}

From source file:TreeTableGradient.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*from   w  w w  . j  a v  a2s .  co  m*/
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    tree.addListener(SWT.EraseItem, new Listener() {
        public void handleEvent(Event event) {
            if ((event.detail & SWT.SELECTED) != 0) {
                GC gc = event.gc;
                Rectangle area = tree.getClientArea();
                /*
                 * If you wish to paint the selection beyond the end of last column,
                 * you must change the clipping region.
                 */
                int columnCount = tree.getColumnCount();
                if (event.index == columnCount - 1 || columnCount == 0) {
                    int width = area.x + area.width - event.x;
                    if (width > 0) {
                        Region region = new Region();
                        gc.getClipping(region);
                        region.add(event.x, event.y, width, event.height);
                        gc.setClipping(region);
                        region.dispose();
                    }
                }
                gc.setAdvanced(true);
                if (gc.getAdvanced())
                    gc.setAlpha(127);
                Rectangle rect = event.getBounds();
                Color foreground = gc.getForeground();
                Color background = gc.getBackground();
                gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
                gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
                // restore colors for subsequent drawing
                gc.setForeground(foreground);
                gc.setBackground(background);
                event.detail &= ~SWT.SELECTED;
            }
        }
    });
    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 322");
    shell.setBounds(10, 10, 300, 300);//  www. j a  v a2s. c om
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL);
    sc.setBounds(10, 10, 280, 200);
    final int clientWidth = sc.getClientArea().width;

    final Tree tree = new Tree(sc, SWT.NONE);
    for (int i = 0; i < 99; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        new TreeItem(item, SWT.NONE).setText("child");
    }
    sc.setContent(tree);
    int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    tree.setSize(clientWidth, prefHeight);
    /*
     * The following listener ensures that the Tree is always large
     * enough to not need to show its own vertical scrollbar.
     */
    tree.addTreeListener(new TreeListener() {
        @Override
        public void treeExpanded(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }

        @Override
        public void treeCollapsed(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }
    });
    /*
     * The following listener ensures that a newly-selected item
     * in the Tree is always visible.
     */
    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selectedItems = tree.getSelection();
        if (selectedItems.length > 0) {
            Rectangle itemRect = selectedItems[0].getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (itemRect.x < origin.x || itemRect.y < origin.y
                    || itemRect.x + itemRect.width > origin.x + area.width
                    || itemRect.y + itemRect.height > origin.y + area.height) {
                sc.setOrigin(itemRect.x, itemRect.y);
            }
        }
    }));
    /*
     * The following listener scrolls the Tree one item at a time
     * in response to MouseWheel events.
     */
    tree.addListener(SWT.MouseWheel, event -> {
        Point origin = sc.getOrigin();
        if (event.count < 0) {
            origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y);
        } else {
            origin.y = Math.max(origin.y - tree.getItemHeight(), 0);
        }
        sc.setOrigin(origin);
    });

    Button disableButton = new Button(shell, SWT.PUSH);
    disableButton.setBounds(10, 220, 120, 30);
    disableButton.setText("Disable");
    disableButton.addListener(SWT.Selection, event -> tree.setEnabled(false));
    Button enableButton = new Button(shell, SWT.PUSH);
    enableButton.setBounds(140, 220, 120, 30);
    enableButton.setText("Enable");
    enableButton.addListener(SWT.Selection, event -> tree.setEnabled(true));

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