• JFace中的表格型树TableTreeViewer


    表格型树是用TableTreeViewer来实现,自从SWT下的TableTree被废弃之后,其扩展TableTreeViewer也成了鸡肋,不再被建议使用,既然Tree可以实现表格型树,那么其扩展TreeView当然也没有问题,用TreeViewer实现的表格型树,之前的TreeViewer实例菜单的呢过功能代码不用做任何修改.

    修改的不多,一是在主程序为树加上几列对象TreeColumn,二是创建一个新的表格型树的标签器,关于TreeColumn在Tree组件的时候有此内容的讲解.这个地方和之前一样.

    TreeViewer3.java

     1 public class TreeViewer3 {
     2     public static void main(String[] args) {
     3         new TreeViewer3().open();
     4     }
     5 
     6     public void open() {
     7         final Display display = new Display();
     8         final Shell shell = new Shell();
     9         shell.setSize(300, 300);
    10         // -----------界面核心代码-------------------
    11         shell.setLayout(new FillLayout());
    12         TreeViewer tv = new TreeViewer(shell, SWT.BORDER);
    13 
    14         Tree tree = tv.getTree();
    15         tree.setHeaderVisible(true);
    16         TreeColumn column = new TreeColumn(tree, SWT.LEFT);
    17         column.setText("名称");
    18         column.setWidth(150);
    19         column = new TreeColumn(tree, SWT.LEFT);
    20         column.setText("子结点数");
    21         column.setWidth(80);
    22         column = new TreeColumn(tree, SWT.LEFT);
    23         column.setText("性别");
    24         column.setWidth(40);
    25 
    26         tv.setContentProvider(new TreeViewerContentProvider());
    27         tv.setLabelProvider(new MyTableLableProvider());
    28         // 和TableViewer一样,数据的入口也是setInput方法
    29         List<Country> input = (List)DataFactory.createTreeData();
    30         tv.setInput(input);
    31         // --------------插入代码:START------------------
    32         MyActionGroup actionGroup = new MyActionGroup(tv);// 生成一个ActionGroup对象
    33         actionGroup.fillContextMenu(new MenuManager());// 将按钮注入到菜单对象中
    34         // -----------END------------------
    35         shell.open();
    36         while (!shell.isDisposed()) {
    37             if (!display.readAndDispatch())
    38                 display.sleep();
    39         }
    40         display.dispose();
    41     }
    42 }

    标签器换了一个,它必须实现ITableLabelProvider接口.

     1 public class MyTableLableProvider implements ITableLabelProvider {
     2     public String getColumnText(Object element, int columnIndex) {
     3         ITreeEntry entry = (ITreeEntry) element;
     4         switch (columnIndex) {
     5         case 0:
     6             return entry.getName();
     7         case 1:
     8             List list = entry.getChildren();
     9             if (list == null)
    10                 return "";
    11             return String.valueOf(list.size());
    12         case 2:
    13             if (entry instanceof People) {
    14                 boolean sex = ((People) entry).isSex();
    15                 return sex ? "男" : "女";
    16             }
    17             return "";
    18         default:
    19             return "";
    20         }
    21     }
    22 
    23     public Image getColumnImage(Object element, int columnIndex) {
    24         return null;
    25     }
    26     public void addListener(ILabelProviderListener listener) {}
    27     public void removeListener(ILabelProviderListener listener) {}
    28     public void dispose() {}
    29     public boolean isLabelProperty(Object element, String property) {
    30         return false;
    31     }
    32 }

  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4178219.html
Copyright © 2020-2023  润新知