Java by API/org.eclipse.swt.widgets/Tree

Материал из Java эксперт
Перейти к: навигация, поиск

new Tree(Composite parent, int style)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A Table Shell Example");
    s.setLayout(new FillLayout());
    Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
    TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Tree: addKeyListener(KeyListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    
    final Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setSize(250, 200);
    
    
    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();
  }
}





Tree: addSelectionListener(SelectionListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A Tree Shell Example");
    s.setLayout(new FillLayout(SWT.HORIZONTAL));
    final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    final TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
//    child1.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    final TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    final List l = new List(s, SWT.BORDER | SWT.SINGLE);
    t.addTreeListener(new TreeListener() {
      public void treeExpanded(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
  //      ti.setImage(new Image(d, "c:\\icons\\open.gif"));
      }
      public void treeCollapsed(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
//        ti.setImage(new Image(d, "c:\\icons\\folder.gif"));
      }
    });
    t.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TreeItem ti = (TreeItem) e.item;
        populateList(ti.getText());
      }
      private void populateList(String type) {
        if (type.equals("1")) {
          l.removeAll();
          l.add("1");
          l.add("2");
        }
        if (type.equals("2")) {
          l.removeAll();
        }
        if (type.equals("2A")) {
          l.removeAll();
          l.add("3");
          l.add("4");
        }
        if (type.equals("2B")) {
          l.removeAll();
          l.add("5");
          l.add("6");
        }
        if (type.equals("3")) {
          l.removeAll();
          l.add("7");
          l.add("8");
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Tree: addTreeListener(TreeListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A Tree Shell Example");
    s.setLayout(new FillLayout(SWT.HORIZONTAL));
    final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    final TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
//    child1.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    final TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    final List l = new List(s, SWT.BORDER | SWT.SINGLE);
    t.addTreeListener(new TreeListener() {
      public void treeExpanded(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
  //      ti.setImage(new Image(d, "c:\\icons\\open.gif"));
      }
      public void treeCollapsed(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
//        ti.setImage(new Image(d, "c:\\icons\\folder.gif"));
      }
    });
    t.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TreeItem ti = (TreeItem) e.item;
        populateList(ti.getText());
      }
      private void populateList(String type) {
        if (type.equals("1")) {
          l.removeAll();
          l.add("1");
          l.add("2");
        }
        if (type.equals("2")) {
          l.removeAll();
        }
        if (type.equals("2A")) {
          l.removeAll();
          l.add("3");
          l.add("4");
        }
        if (type.equals("2B")) {
          l.removeAll();
          l.add("5");
          l.add("6");
        }
        if (type.equals("3")) {
          l.removeAll();
          l.add("7");
          l.add("8");
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Tree: getSelection()

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    
    final Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setSize(250, 200);
    
    
    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();
  }
}