JMeter里JTree的CellRender代码如下:public class JMeterCellRenderer extends DefaultTreeCellRenderer {
public JMeterCellRenderer() {
} public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean p_hasFocus) {
super.getTreeCellRendererComponent(tree, ((JMeterTreeNode) value).getName(), sel, expanded, leaf, row,
p_hasFocus);
boolean enabled = ((JMeterTreeNode) value).isEnabled();
ImageIcon ic = ((JMeterTreeNode) value).getIcon(enabled);
if (ic != null) {
if (enabled) {
setIcon(ic);
} else {
setDisabledIcon(ic);
}
} else {
if (!enabled)// i.e. no disabled icon found
{
// Must therefore set the enabled icon so there is at least some
// icon
ic = ((JMeterTreeNode) value).getIcon();
if (ic != null) {
setIcon(ic);
}
}
}
this.setEnabled(enabled);
return this;
}
}就是根据树的每个结点的内容及开合状态来设置它的图标。JMeterTreeNode的getIcon方法代码如下: public ImageIcon getIcon(boolean enabled) {
TestElement testElement = getTestElement();
try {
if (testElement instanceof TestBean) {
Class testClass = testElement.getClass();
try {
Image img = Introspector.getBeanInfo(testClass).getIcon(BeanInfo.ICON_COLOR_16x16);
// If icon has not been defined, then use GUI_CLASS property
if (img == null) {
Object clazz = Introspector.getBeanInfo(testClass).getBeanDescriptor()
.getValue(TestElement.GUI_CLASS);
if (clazz == null) {
log.warn("getIcon(): Can't obtain GUI class from " + testClass.getName());
return null;
}
return GUIFactory.getIcon(Class.forName((String) clazz), enabled);
}
return new ImageIcon(img);
} catch (IntrospectionException e1) {
log.error("Can't obtain icon for class "+testElement, e1);
throw new org.apache.jorphan.util.JMeterError(e1);
}
}
return GUIFactory.getIcon(Class.forName(testElement.getPropertyAsString(TestElement.GUI_CLASS)),
enabled);
} catch (ClassNotFoundException e) {
log.warn("Can't get icon for class " + testElement, e);
return null;
}
}我的问题出来了,Introspector这个东西是干嘛用的,API上写的很笼统,大虾们给个通俗点儿的解释,最好
贴点儿初级的应用代码出来。谢哈!!!
public JMeterCellRenderer() {
} public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean p_hasFocus) {
super.getTreeCellRendererComponent(tree, ((JMeterTreeNode) value).getName(), sel, expanded, leaf, row,
p_hasFocus);
boolean enabled = ((JMeterTreeNode) value).isEnabled();
ImageIcon ic = ((JMeterTreeNode) value).getIcon(enabled);
if (ic != null) {
if (enabled) {
setIcon(ic);
} else {
setDisabledIcon(ic);
}
} else {
if (!enabled)// i.e. no disabled icon found
{
// Must therefore set the enabled icon so there is at least some
// icon
ic = ((JMeterTreeNode) value).getIcon();
if (ic != null) {
setIcon(ic);
}
}
}
this.setEnabled(enabled);
return this;
}
}就是根据树的每个结点的内容及开合状态来设置它的图标。JMeterTreeNode的getIcon方法代码如下: public ImageIcon getIcon(boolean enabled) {
TestElement testElement = getTestElement();
try {
if (testElement instanceof TestBean) {
Class testClass = testElement.getClass();
try {
Image img = Introspector.getBeanInfo(testClass).getIcon(BeanInfo.ICON_COLOR_16x16);
// If icon has not been defined, then use GUI_CLASS property
if (img == null) {
Object clazz = Introspector.getBeanInfo(testClass).getBeanDescriptor()
.getValue(TestElement.GUI_CLASS);
if (clazz == null) {
log.warn("getIcon(): Can't obtain GUI class from " + testClass.getName());
return null;
}
return GUIFactory.getIcon(Class.forName((String) clazz), enabled);
}
return new ImageIcon(img);
} catch (IntrospectionException e1) {
log.error("Can't obtain icon for class "+testElement, e1);
throw new org.apache.jorphan.util.JMeterError(e1);
}
}
return GUIFactory.getIcon(Class.forName(testElement.getPropertyAsString(TestElement.GUI_CLASS)),
enabled);
} catch (ClassNotFoundException e) {
log.warn("Can't get icon for class " + testElement, e);
return null;
}
}我的问题出来了,Introspector这个东西是干嘛用的,API上写的很笼统,大虾们给个通俗点儿的解释,最好
贴点儿初级的应用代码出来。谢哈!!!
楼主【ganhaitian】截止到2008-07-26 01:47:45的历史汇总数据(不包括此帖):
发帖的总数量:15 发帖的总分数:350 每贴平均分数:23
回帖的总数量:12 得分贴总数量:4 回帖的得分率:33%
结贴的总数量:15 结贴的总分数:350
无满意结贴数:3 无满意结贴分:140
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:20.00 % 无满意结分率:40.00 %
敬礼!
/**
* 内省演示例子
*
*/public class IntrospectorDemo {
String name;
public static void main(String[] args) throws Exception{
IntrospectorDemo demo = new IntrospectorDemo();
demo.setName( "Winter Lau" ); // 如果不想把父类的属性也列出来的话,
// 那 getBeanInfo 的第二个参数填写父类的信息
BeanInfo bi = Introspector.getBeanInfo(demo.getClass(), Object. class );
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for ( int i=0;i<props.length;i++){
System.out.println(props[i].getName()+ "=" +
props[i].getReadMethod().invoke(demo, null ));
} } public String getName() {
return name;
} public void setName(String name) {
this .name = name;
}
}
而借助于自省机制呢?这种方法有什么优越性吗?