Java Tutorial/SWT/Tree

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

Add checkbox mark to the tree node

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeNodeCheckBoxAdd {
  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);
      for(int l = 0;l<12;l++){
        TreeItem litem = new TreeItem(item, SWT.NONE);
        litem.setText("Item " + i);
      }
      
    }
    tree.setSize(100, 100);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





A multiselection tree

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
/**
 * Displays a single-selection tree, a multiselection tree, and a checkbox tree
 */
public class TreeMultiSelection {
  

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("TreeExample");
    Tree tree = new Tree(shell, SWT.MULTI | SWT.BORDER);
    // Turn off drawing to avoid flicker
    tree.setRedraw(false);
    for (int i = 0; i < 5; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("Root Item " + i);
      for (int j = 0; j < 3; j++) {
        TreeItem child = new TreeItem(item, SWT.NONE);
        child.setText("Child Item " + i + " - " + j);
        for (int k = 0; k < 3; k++) {
          TreeItem grandChild = new TreeItem(child, SWT.NONE);
          grandChild.setText("Grandchild Item " + i + " - " + j + " - " + k);
        }
      }
    }
    tree.setRedraw(true);
      
    tree.setBounds(10,10,400,400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





A single-selection tree

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 TreeSingleSelection {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("TreeExample");
    Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER);
    // Turn off drawing to avoid flicker
    tree.setRedraw(false);
    for (int i = 0; i < 5; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("Root Item " + i);
      for (int j = 0; j < 3; j++) {
        TreeItem child = new TreeItem(item, SWT.NONE);
        child.setText("Child Item " + i + " - " + j);
        for (int k = 0; k < 3; k++) {
          TreeItem grandChild = new TreeItem(child, SWT.NONE);
          grandChild.setText("Grandchild Item " + i + " - " + j + " - " + k);
        }
      }
    }
    tree.setRedraw(true);
      
    tree.setBounds(10,10,400,400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Create a Tree

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 TreeCreate {
  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);
    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);
            for (int m = 0; m < 4; m++) {
              TreeItem mItem = new TreeItem(lItem, 0);
              mItem.setText("TreeItem (3) -" + l);
            }
            
          }
        }
      }
    }
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Create Three Level Tree

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 TreeThreeLevel {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("item " + i);
      for (int j = 0; j < 3; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        subItem.setText("item " + i + " " + j);
        for (int k = 0; k < 3; k++) {
          TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
          subsubItem.setText("item " + i + " " + j + " " + k);
        }
      }
    }
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Insert TreeItem to Tree

import org.eclipse.swt.SWT;
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 TreeItemInsert {
  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);
    for (int i = 0; i < 5; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText("Item " + i);
    }
    
    TreeItem item = new TreeItem(tree, SWT.NONE, 1);
    item.setText("*** New Item ***");
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Populate tree lazily

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Virtual Tree example snippet: populate tree lazily
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreePopulateLazy {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER);
    tree.addListener(SWT.SetData, new Listener() {
      public void handleEvent(Event event) {
        TreeItem item = (TreeItem) event.item;
        TreeItem parentItem = item.getParentItem();
        String text = null;
        if (parentItem == null) {
          text = "node " + tree.indexOf(item);
        } else {
          text = parentItem.getText() + " - " + parentItem.indexOf(item);
        }
        item.setText(text);
        item.setItemCount(10);
      }
    });
    tree.setItemCount(20);
    shell.setSize(400, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Trees

SWT uses two classes, Tree and TreeItem, to implement tree controls.

Creating Trees



public Tree(Composite parent, int style)



Trees can allow either a single selection or multiple selections.

Tree Styles

StyleDescriptionSWT.SINGLEAllows only one item in the tree to be selected at a time. This is the default.SWT.MULTIAllows multiple items in the tree to be selected at the same time.SWT.CHECKDisplays a checkbox to the left of each of the root items in the tree.