Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:30:54+00:00 2026-05-26T13:30:54+00:00

Now I am developing a sample plugin for Eclipse, to show all directories and

  • 0

Now I am developing a sample plugin for Eclipse, to show all directories and files from my computer, using JFace (Tree and TreeView).

I want something like this:

----------------------------------
| Element  | Number of Childrens |
----------------------------------
| + usr    | 5                   |
|  + bin   | 4                   |
|     file |                     |
|  + games | 2                   |
----------------------------------

I create the plugin View witch ViewPart, the ContentProvider and the TreeLabelProvider.

public class ListFiles extends ViewPart {
    /** The ID of the view as specified by the extension. */
    public static final String ID = "sortnumbers.views.SortNumbers";

    /**
     * The constructor.
     */
    public ListFiles() { }

    /**
     * This is a callback that will allow us to create the viewer and initialize it
     */
    public void createPartControl(Composite parent) {
        Tree tree = new Tree(parent, SWT.MULTI);
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);

        TreeColumn column0 = new TreeColumn(tree, SWT.NONE);
        column0.setText("Element");
        column0.setWidth(300);
        TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
        column1.setText("Number");
        column1.setWidth(300);

        TreeViewer treeviewer = new TreeViewer(tree);
        treeviewer.setContentProvider(new TreeContentProvider());
        treeviewer.setLabelProvider(new TreeLabelProvider());
        treeviewer.setInput(new FolderNode(new File("/")));
    }

    @Override
    public void setFocus() {
        // TODO
    }
}

public class TreeContentProvider implements ITreeContentProvider
{
    public Object[] getChildren(Object parentElement) {
        return ((ITreeNode)parentElement).getChildren().toArray();
    }

    public Object getParent(Object element) {
        return ((ITreeNode)element).getParent();
    }

    public boolean hasChildren(Object element) {
        return ((ITreeNode)element).hasChildren();
    }

    public Object[] getElements(Object inputElement) {
        return getChildren(inputElement);
    }

    public void dispose() {
    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }
}

public class TreeLabelProvider extends LabelProvider
{
    public String getText(Object element) {
        return ((ITreeNode)element).getName();
    }
}

To search for all Files in my computer I create the interface ITreeNode, TreeNode, FolderNode and FileNode…

public interface ITreeNode
{
    public String getName();

    public List getChildren();

    public boolean hasChildren();

    public ITreeNode getParent();
}

public abstract class TreeNode implements ITreeNode
{
    protected ITreeNode fParent;
    protected List fChildren;

    public TreeNode(ITreeNode parent) {
        fParent = parent;
    }

    public boolean hasChildren() {
        return true;
    }

    public ITreeNode getParent() {
        return fParent;
    }

    public List getChildren() 
    {
        if( fChildren != null )
            return fChildren;

        fChildren = new ArrayList();
        createChildren(fChildren);

        return fChildren;
    }

    /* subclasses should override this method and add the child nodes */
    protected abstract void createChildren(List children);
}

public class FolderNode extends TreeNode
{
    private File fFolder; /* actual data object */

    public FolderNode(File folder) {
        this(null, folder);
    }

    public FolderNode(ITreeNode parent, File folder) {
        super(parent);
        fFolder = folder;
    }

    public String getName() {
        return "FOLDER: " + fFolder.getName();
    }

    @Override
    protected void createChildren(List children)
    {
        File[] childFiles = fFolder.listFiles();
        for(int i=0; i<childFiles.length; i++)
        {
            File childFile = childFiles[i];
            if( childFile.isDirectory() )
                children.add(new FolderNode(this, childFile));
            else
                children.add(new FileNode(this, childFile));
        }
    }
}

public class FileNode extends TreeNode
{
    private File fFile; /* actual data object */

    public FileNode(ITreeNode parent, File file)
    {
        super(parent);
        fFile = file;
    }

    public String getName() {
        return "FILE: " + fFile.getName();
    }

    protected void createChildren(List children) {
    }

    public boolean hasChildren() {
        return false;
    }
}

But the result was:

----------------------------------
| Element  | Number of Childrens |
----------------------------------
| + usr    | + usr               |
|  + bin   |  + bin              |
|     file |     file            |
|  + games |  + games            |
----------------------------------

I cannot understand why TreeView fill all fields with the same string… What I need to change?

—
Thanks in advance


Alexey Romanov, thank you… 🙂

Now I need to sort all elements in the TreeView by column (by name for the first column and by size for the second column). Should I create a Class extended from ViewerComparator or ViewerSorter?
And how can implement compare method to do this?

—
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:30:54+00:00Added an answer on May 26, 2026 at 1:30 pm

    You need to use ITableLabelProvider:

    public class TreeLabelProvider extends BaseLabelProvider implements ITableLabelProvider
    {
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    
        public String getColumnText(Object element, int columnIndex) {
            if (columnIndex == 0)
                return ((ITreeNode)element).getName();
            else
                return ((ITreeNode)element).getChildren().size();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I come from Windows-VisualStudio-C++ background. Now I am developing in a Ubuntu environment.
I'm developing a facebook app right now all by my lonesome. I'm attempting to
I am developing an application where i am using TabHost.Now , at launching of
I'm developing a Solr plugin and using the Solr test-framework I place a test
I have been developing simple web services and clients using JBoss but now I
I am right now developing a dead simple application using backbonejs mvc javascript library.Just
I have 4 years experience on DotNet, not much. I now learned developing for
Right now I'm developing mostly in C/C++, but I wrote some small utilities in
Right now I'm developing a small canvas oriented 2D graphics engine for a game,
i'm new at IPhone SDK Developing and now i need some help. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.