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:TreeNodeSelectionListener.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
    for (int i = 0; i < 4; i++) {
        TreeItem item0 = new TreeItem(tree, 0);
        item0.setText("Item " + i);
        for (int j = 0; j < 4; j++) {
            TreeItem item1 = new TreeItem(item0, 0);
            item1.setText("SubItem " + i + " " + j);
            for (int k = 0; k < 4; k++) {
                TreeItem item2 = new TreeItem(item1, 0);
                item2.setText("SubItem " + i + " " + j + " " + k);
            }/*w  w w. ja  v  a 2 s.c om*/
        }
    }
    tree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            String string = "";
            TreeItem[] selection = tree.getSelection();
            for (int i = 0; i < selection.length; i++)
                string += selection[i] + " ";
            System.out.println("Selection={" + string + "}");
        }
    });
    tree.getItems()[0].setExpanded(true);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet15.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    final Tree tree = new Tree(shell, SWT.BORDER);
    tree.setSize(100, 100);/*w w w .  j ava 2  s . c om*/
    shell.setSize(200, 200);
    for (int i = 0; i < 4; i++) {
        TreeItem iItem = new TreeItem(tree, 0);
        iItem.setText("TreeItem (0) -" + i);
        for (int j = 0; j < 4; j++) {
            TreeItem jItem = new TreeItem(iItem, 0);
            jItem.setText("TreeItem (1) -" + j);
            for (int k = 0; k < 4; k++) {
                TreeItem kItem = new TreeItem(jItem, 0);
                kItem.setText("TreeItem (2) -" + k);
                for (int l = 0; l < 4; l++) {
                    TreeItem lItem = new TreeItem(kItem, 0);
                    lItem.setText("TreeItem (3) -" + l);
                }
            }
        }
    }
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet102.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    tree.setSize(200, 200);/*from  w w  w  .ja va2  s . c  o  m*/
    for (int i = 0; i < 12; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item " + i);
    }
    TreeItem item = new TreeItem(tree, SWT.NONE, 1);
    TreeItem[] items = tree.getItems();
    int index = 0;
    while (index < items.length && items[index] != item)
        index++;
    item.setText("*** New Item " + index + " ***");
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeNodeDefaultSelectionListener.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
    for (int i = 0; i < 4; i++) {
        TreeItem item0 = new TreeItem(tree, 0);
        item0.setText("Item " + i);
        for (int j = 0; j < 4; j++) {
            TreeItem item1 = new TreeItem(item0, 0);
            item1.setText("SubItem " + i + " " + j);
            for (int k = 0; k < 4; k++) {
                TreeItem item2 = new TreeItem(item1, 0);
                item2.setText("SubItem " + i + " " + j + " " + k);
            }/*www  . j  av a 2 s  . c om*/
        }
    }
    tree.addListener(SWT.DefaultSelection, new Listener() {
        public void handleEvent(Event e) {
            String string = "";
            TreeItem[] selection = tree.getSelection();
            for (int i = 0; i < selection.length; i++)
                string += selection[i] + " ";
            System.out.println("DefaultSelection={" + string + "}");
        }
    });

    tree.getItems()[0].setExpanded(true);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeClass.java

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

    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(0, 270, 290, 25);/* ww w. jav a2s .co  m*/

    final Tree tree = new Tree(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    tree.setSize(290, 260);
    shell.setSize(300, 330);

    for (int loopIndex0 = 0; loopIndex0 < 10; loopIndex0++) {
        TreeItem treeItem0 = new TreeItem(tree, 0);
        treeItem0.setText("Level 0 Item " + loopIndex0);
        for (int loopIndex1 = 0; loopIndex1 < 10; loopIndex1++) {
            TreeItem treeItem1 = new TreeItem(treeItem0, 0);
            treeItem1.setText("Level 1 Item " + loopIndex1);
            for (int loopIndex2 = 0; loopIndex2 < 10; loopIndex2++) {
                TreeItem treeItem2 = new TreeItem(treeItem1, 0);
                treeItem2.setText("Level 2 Item " + loopIndex2);
            }
        }
    }

    tree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (event.detail == SWT.CHECK) {
                text.setText(event.item + " was checked.");
            } else {
                text.setText(event.item + " was selected");
            }
        }
    });

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

From source file:Snippet114.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Tree tree = new Tree(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    for (int i = 0; i < 12; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item " + i);
    }//from ww  w  .  j  av  a 2s.c o  m
    tree.setSize(100, 100);
    tree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            String string = event.detail == SWT.CHECK ? "Checked" : "Selected";
            System.out.println(event.item + " " + string);
        }
    });
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:CreateTreeWithcolumns.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    tree.setHeaderVisible(true);/*w  w w  . j a  va2s. c o  m*/
    TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
    column1.setText("Column 1");
    column1.setWidth(200);
    TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
    column2.setText("Column 2");
    column2.setWidth(200);
    TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
    column3.setText("Column 3");
    column3.setWidth(200);
    for (int i = 0; i < 4; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(new String[] { "item " + i, "abc", "defghi" });
        for (int j = 0; j < 4; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText(new String[] { "subitem " + j, "jklmnop", "qrs" });
            for (int k = 0; k < 4; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText(new String[] { "subsubitem " + k, "tuv", "wxyz" });
            }
        }
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:TreeEditorF2Trigger.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Text Tree Editor");
    shell.setLayout(new FillLayout());

    final Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
        TreeItem iItem = new TreeItem(tree, SWT.NONE);
        iItem.setText("Item " + (i + 1));
        for (int j = 0; j < 3; j++) {
            TreeItem jItem = new TreeItem(iItem, SWT.NONE);
            jItem.setText("Sub Item " + (j + 1));
            for (int k = 0; k < 3; k++) {
                new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
            }/*from   w  w  w.  j av  a2 s  . co m*/
            jItem.setExpanded(true);
        }
        iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent event) {
            if (event.keyCode == SWT.F2 && tree.getSelectionCount() == 1) {
                final TreeItem item = tree.getSelection()[0];

                int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

                MessageBox messageBox = new MessageBox(shell, style);
                messageBox.setMessage("Message");
                int rc = messageBox.open();

                item.setText(rc + "");
            }
        }
    });

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

From source file:SWTTrees.java

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

    final Tree tree = new Tree(shell, SWT.BORDER);
    tree.setSize(290, 290);//from  ww w .jav  a 2  s  .  c o  m
    shell.setSize(300, 300);

    for (int loopIndex1 = 0; loopIndex1 < 5; loopIndex1++) {
        TreeItem item0 = new TreeItem(tree, 0);
        item0.setText("Level 0 Item " + loopIndex1);
        for (int loopIndex2 = 0; loopIndex2 < 5; loopIndex2++) {
            TreeItem item1 = new TreeItem(item0, 0);
            item1.setText("Level 1 Item " + loopIndex2);
            for (int loopIndex3 = 0; loopIndex3 < 5; loopIndex3++) {
                TreeItem item2 = new TreeItem(item1, 0);
                item2.setText("Level 2 Item " + loopIndex3);
            }
        }
    }

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

From source file:MainClass.java

public static void main(String[] a) {

    final Display d = new Display();
    final Shell shell = new Shell(d);

    shell.setSize(250, 200);//from  w w w  .j  a  v  a2  s .  c om

    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.SINGLE);

    for (int i = 0; i < 3; i++) {
        TreeItem iItem = new TreeItem(tree, SWT.NONE);
        iItem.setText("Edit me by pressing F2 " + (i + 1));
        iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent event) {
            if (event.keyCode == SWT.F2 && tree.getSelectionCount() == 1) {
                final TreeItem item = tree.getSelection()[0];

                final Text text = new Text(tree, SWT.NONE);
                text.setText(item.getText());
                text.setFocus();

                text.addFocusListener(new FocusAdapter() {
                    public void focusLost(FocusEvent event) {
                        item.setText(text.getText());
                        text.dispose();
                    }
                });

                editor.setEditor(text, item);
            }
        }
    });

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

}