TableViewer and TreeViewer

    技术2022-05-11  22

    TableViewer and TreeViewer are JFace viewers. In eclipse, Problems view and Properties view are mainly implemented by TabelViewer. Outline view and Navigator view are implemented by TreeViewer.

     

    In working with JFace viewers, it's really import to understand the concept of  content provider and label provider. Briefly speaking, content provider provides what to show, label provider provides how to show. Now, I'll introduce them respectively.

     

    TableViewer

     

    For TabelViewer, the content provider should implement IStructuredContentProvider. This interface has three method need to be implemented. They are

    public Object[] getElements(Object inputElement) {} // usually the returned object array is obtained from // the input Element; public void dispose() {} public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} // when input is changed by using "TableViewer.setInput(newElement)", // newElement will be passed to parameter "newInput" here. Finally, // it will go to method "getElements(Object)" as the only parameter.

     

    Then, the label provider should inherite ITableLabelProvider. It provides two APIs.

    public Image getColumnImage(Object element, int columnIndex) {} // It provides the image at the head of each row. public String getColumnText(Object element, int columnIndex) {} // It provides the text to show at a specified "columnIndex", // Paramter element is one member in the object array provided // by the content provider.

     

    TreeViewer

    Content provider should use ITreeContentProvider. It provides two more methods than ITableContentProvider. They are getParent() and getChildren(). They tell the relationship between different elements. As the lazy spirit, it only shows what need to be shown. When a parent is folded, its children are still unknown. The time you click on the "+" sign, getChildren() is invoked.

     

    Label provider should implement ILabelProvider. Obviously, a label in the tree doesn't have columns. So the getText(Object) doesn't have the second parameter.

     

    Link : Eclipse JFace TableViewer - Tutorial

     


    最新回复(0)