2010-09-01 110 views
0

嗨,大家好我有一个问题让我很困惑,我有'writeMethod'这是一个Method类,'dpv'是一个propertyDescriptor类型,我通过getWriteMethod()获取了对象的writeMethod,我现在的问题是如何设置“writeMethod”对一个对象(例如一个JLabel,JButton的)的属性写在这里是我的代码:使用writeMethod写入对象的属性?

if(dpv.getPropertyType().isPrimitive() 
     || dpv.getPropertyType().isInstance("Integer")) 
     { 
      Method writeMethod = dpv.getWriteMethod(); 

      //setWriteMethod(writeMethod);<---------- Not sure about this part (doesn't work) 

      System.out.println(writeMethod); 
      PropertyValue.setEnabled(true); 
      SetButton.setEnabled(true); 
     } 
     else{ 

      PropertyValue.setEnabled(false); 
      SetButton.setEnabled(false); 
     } 

感谢您的帮助球员

回答

1

要使用的方法编写这个propery,你必须调用它。简单的属性采用一个值 - 属性的值,所以你用一个参数调用该方法。下面的代码设置按钮上的属性值42:

Method writeMethod = dpv.getWriteMethod();  
JButton button = ...; // the target to write to 

try 
{ 
    writeMethod.invoke(button, 42); 
} 
catch (IllegalAccessException ex) 
{ 
    // handle these as appropriate 
} 
catch (IllegalArgumentException ex) 
{ 
} 
catch (InvocationTargetException ex) 
{ 
} 

这是不太可能你有他们,但如果该属性是很少使用的索引属性类型,那么你需要使用的方法是这样的:

writeMethod.invoke(target, index, propertyValue); 

这相当于setter方法

setIndexProperty(int index, PropertyType value); 
+0

感谢它帮助 – HAMID 2010-09-01 10:27:23