Swing コンポーネントを使用する際のバインドされたプロパティの使用について質問があります。したがって、JButton クラスを拡張するこの非常に単純な Java クラスがあります。
public class MyBean extends JButton
{
public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
private String sampleProperty;
public MyBean() {
addPropertyChangeListener(new java.beans.PropertyChangeListener()
{
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
componentPropertyChange(evt);
}
});
}
public String getSampleProperty() {
return sampleProperty;
}
public void setSampleProperty(String value) {
String oldValue = sampleProperty;
sampleProperty = value;
firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
}
// Handle a property being updated
static private void componentPropertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
System.out.println(MyBean.class.getSimpleName() + " - '" + propertyName + "' changed");
}
}
これが私のメインクラスです:
public static void main(String[] args) {
try
{
Class<?> clazz = MyBean.class;
BeanInfo bi = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (PropertyDescriptor pd : pds)
{
System.out.println(String.format("%s - %s - %s", clazz.getSimpleName(), pd.getName(), Boolean.toString(pd.isBound())));
}
MyBean myBean = new MyBean();
myBean.setText("My name");
myBean.setLocation(new Point(10,10));
myBean.setVisible(true);
}
catch (IntrospectionException e)
{
e.printStackTrace();
}
}
このアプリケーションを実行すると、次のように出力されます。
MyBean - UI - true
MyBean - UIClassID - true
MyBean - accessibleContext - true
MyBean - action - true
...
MyBean - vetoableChangeListeners - true
MyBean - visible - true
MyBean - visibleRect - true
MyBean - width - true
MyBean - x - true
MyBean - y - true
**MyBean - 'text' changed**
私の質問:
すべての MyBean プロパティについて、プロパティがバインドされていることを示す行が出力されますが、MyBean インスタンスを作成し、setText、setLocation、および setVisible を呼び出すと、setText の場合にのみ行が出力されます。私の結論は、setLocation と setVisible が呼び出された場合、firePropertyChangeメソッドが呼び出されないため、PropertyChangedListenerが呼び出されないということです (おそらく間違っています)。ここで、バインドされたすべてのプロパティが更新されると、 firePropertyChangeメソッドが呼び出されると思いましたか? どうやらこれは当てはまらないか、印刷されたリストが間違っている可能性があります。これについての説明は大歓迎です。