Search code examples
javaeclipseeclipse-rcpe4

filename is displayed in tableviewer but when selected particular column the file location should be retrieved for processing the file in eclipse e4?


Using Filedialog I am selecting the set of files and displaying it in the checkbox table viewer. Then I want to process those files which I have checked in the checkboxtableviewer.

public void setTableInput(File[] selectedFiles) {

    for(int i = 0;i <selectedFiles.length; i++)
    {
        tableViewer.add(selectedFiles[i].getName());                
    }
    tableViewer.addCheckStateListener(new ICheckStateListener() {

        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            Object[] filesSelected = tableViewer.getCheckedElements();
            for(Object filename : filesSelected){
                System.out.println("values "+ (String)filename);
            }
        }
    });
}

For the code that I have written over here, I can get only the file names , Can anyone please tell me how to proceed if I want to get file location based on selecting the filename in checkbox?

Thanks in advance


Solution

  • You need to use a content and label provider so you can set the File array as the table input.

    public void setTableInput(File[] selectedFiles) 
    {
      tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    
      tableViewer.setLabelProvider(new FileLabelProvider());
    
      tableViewer.setInput(selectedFiles);
    
      tableViewer.addCheckStateListener(new ICheckStateListener() 
      {
        @Override
        public void checkStateChanged(CheckStateChangedEvent event)
        {
          Object[] filesSelected = tableViewer.getCheckedElements();
    
          for (Object fileObj : filesSelected)
           {
             File file = (File)fileObj;
    
             System.out.println("values "+ file.getPath());
           }
        }
      });
    }    
    
    
    private static class FileLabelProvider extends LabelProvider
    {
      @Override
      public String getText(final Object element)
      {
        File file = (File)element;
    
        return file.getName();
      }
    }