java - Render index of duplicate elements at table using JFace -


i have been trying render table using jface. issue 1 of columns render holds index of element inside array. follwoing:

tableviewercolumn tableviewercolumn = new tableviewercolumn(tableviewer, swt.none); tableviewercolumn.setlabelprovider(new columnlabelprovider() {           public string gettext(object object) {         //employeenames list of string         return string.valueof(company.getemployeenames().indexof(object)+ 1);     } }); 

this return first index occurance in case there dublicates. don't have luxury alter type of employeenames.

can give me hint handle index issue!

solution 1: index of cell

here small class can used column label provider show row number in jface tableviewer.

basically not index of object associated cell, cell itself.

import java.util.arrays; import org.eclipse.jface.viewers.celllabelprovider; import org.eclipse.jface.viewers.columnviewer; import org.eclipse.jface.viewers.tableviewer; import org.eclipse.jface.viewers.viewercell; import org.eclipse.jface.viewers.viewercolumn;  public class rownumberlabelprovider extends celllabelprovider {      private tableviewer viewer;      protected void initialize(columnviewer viewer, viewercolumn column) {         super.initialize(viewer, column);         this.viewer = null;         if (viewer instanceof tableviewer) {             this.viewer = (tableviewer) viewer;         }     }      public void update(viewercell cell) {         super.update(cell);         if (viewer != null) {             int index = arrays.aslist(viewer.gettable().getitems()).indexof(cell.getitem());             cell.settext("" + (index + 1));         }     } } 

to use it, set label provider of column

tableviewercolumn tableviewercolumn = new tableviewercolumn(tableviewer, swt.none); tableviewercolumn.setlabelprovider(new rownumberlabelprovider()); 

i'm not sure if work when there filters enabled or table sorted.


solution 2: search reference

as alternative try object reference:

 tableviewercolumn tableviewercolumn = new tableviewercolumn(tableviewer, swt.none);  tableviewercolumn.setlabelprovider(new columnlabelprovider() {       public string gettext(object object) {          int index = 1;          (string employee : company.getemployeenames()) {              if (employee == object)                   return string.valueof(index);              index++;          }          return "";      } }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -