Using reflection to realize the Itablelabelprovider interface of Tableviewer in SWT

Source: Internet
Author: User
Tags getdate reflection

import Java.lang.reflect.Field;
import org.eclipse.jface.viewers.ILabelProviderListener;
Import Org.eclipse.jface.viewers.ITableLabelProvider;
Import org.eclipse.swt.graphics.Image;

/**
* @author House
*
* TODO To change the template for this generated type annotation, go to the
* window-Preferences-Java-code styles-code Templates
*/
Publ IC class Tableviewerlabelprovider implements Itablelabelprovider {
/* (non-Javadoc)
* @see Org.eclipse.jface.viewe Rs. Itablelabelprovider#getcolumnimage (java.lang.Object, int)
*/
Public Image getcolumnimage (Object element, int ColumnIndex) {
//TODO automatically generate method stubs
return null;
}  
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.itablelabelprovider#getcolumntext (java.lang.Object, int)
*/
Public String Getcolumntext (Object element, int columnindex) {
//TODO automatically generate method stub

Class PE = null;< br> Pe=element.getclass ();
Field[] Field=pe.getfields ();  
try {
return Field[columnindex].get (Element). toString ();
  catch (IllegalArgumentException E1) {
//TODO automatically generates catch blocks
E1.printstacktrace ();
catch (Illegalaccessexception E1) {
//TODO automatically generate catch blocks
E1.printstacktracE ();
}


//if (columnindex==0) {return P.getid (). toString ();
//if (columnindex==1) {return p.getname ();}
//if (columnindex==2) {return p.getsex ()? "Man": "Woman";}
//if (columnindex==3) {return p.getdate (). toString ();
Return "";

}

/* (非 Javadoc)
  * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
  */
public void addListener(ILabelProviderListener listener) {
  // TODO 自动生成方法存根

}
/* (非 Javadoc)
  * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
  */
public void dispose() {
  // TODO 自动生成方法存根

}

/* (非 Javadoc)
  * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
  */
public boolean isLabelProperty(Object element, String property) {
  // TODO 自动生成方法存根
  return false;
}

/* (非 Javadoc)
  * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
  */
public void removeListener(ILabelProviderListener listener) {
  // TODO 自动生成方法存根

}
}

Use

Class pe = null;
  pe=element.getClass();
  Field[] field=pe.getFields();
  return field[columnIndex].get(element).toString();
取代原有的// if(columnIndex==0){return p.getId().toString();}
// if(columnIndex==1){return p.getName();}
// if(columnIndex==2){return p.getSex()? "man":"woman";}
// if(columnIndex==3){return p.getDate().toString();}

This class is also not required to be modified when multiple classes are invoked. However, it should be noted that the view should include the entire class field of the persisted class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.