我正在为我的bean编写定制器类。我的bean有两个属性:
我班级的来源:
public class MyBeanCustomizer extends JTabbedPane implements Customizer {
private PropertyEditor colorEditor;
private PropertyEditor sizeEditor;
private MyBean myBean;
public MyBeanCustomizer() {
colorEditor = PropertyEditorManager.findEditor(Color.class);
sizeEditor = PropertyEditorManager.findEditor(Dimension.class);
}
@Override
public void setObject(Object bean) {
this.myBean = (MyBean) bean;
colorEditor.setValue(myBean.getColor());
sizeEditor.setValue(myBean.getSize());
initialize();
}
private void initialize() {
JPanel colorPane = new JPanel();
colorEditor.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setColor((Color) colorEditor.getValue());
}
});
colorPane.add(colorEditor.getCustomEditor());
JPanel sizePane = new JPanel();
sizeEditor.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setSize((Dimension) sizeEditor
.getValue());
}
});
sizePane.add(sizeEditor.getCustomEditor());
addTab("Color", colorPane);
addTab("Size", sizePane);
}
public void setColor(Color newValue) {
if (myBean == null)
return;
Color oldValue = myBean.getColor();
myBean.setColor(newValue);
firePropertyChange("color", oldValue, newValue);
}
public void setSize(Dimension newValue) {
if (myBean == null)
return;
Dimension oldValue = myBean.getSize();
myBean.setSize(newValue);
firePropertyChange("size", oldValue, newValue);
}
}
当我尝试打开我的自定义程序时,我得到了这个例外:
java.lang.NullPointerException
at org.netbeans.beaninfo.editors.PointCustomEditor.commonInit(PointCustomEditor.java:118)
at org.netbeans.beaninfo.editors.PointCustomEditor.<init>(PointCustomEditor.java:103)
at org.netbeans.beaninfo.editors.DimensionEditor.getCustomEditor(DimensionEditor.java:105)
at MyBeanCustomizer.initialize(MyBeanCustomizer.java:49)
at MyBeanCustomizer.setObject(MyBeanCustomizer.java:28)
at org.netbeans.modules.form.RADComponentNode.createCustomizer(RADComponentNode.java:490)
...
正如您所看到的,内部NetBeans类中出现问题。它是NetBeans的错误吗?我在setObject()中设置sizeEditor的值,所以一切都应该没问题。 colorEditor效果很好。