BeansBindingを勉強するためのメモ
2007/12/09
2008/11/14
beansbinding
java
swingx
The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 295
【コラム】Java API、使ってますか? (6) JavaBeansのプロパティを同期させるバインディングAPI | エンタープライズ | マイコミジャーナル
画面系のアプリケーションでデータと画面を結びつけるパターンは2種類あるかなと思いました。
1.コレクト型(収集タイプ)
2.バインド型
1.の場合は、何かアクションがおきたら、画面からデータをひろい集めて、処理をする。
2.はデータと画面の項目が対になっている。
1.の場合は2.よりちょっと雑な感じがする。すごく大雑把な動きをするイメージ。
と考えている途中。
SwingXのorg.jdesktop.beans.AbstractBeanを使ってBeans Bindingの仕組みを理解していくための準備。
org.jdesktop.beans.AbstractBeanは、non-visualなbeanでもPropertyChangeイベントの仕組みが使えるようにしてくれます。といっても全部自動で何かしてくれるわけではなくて、イベントを発火させるコードは書かないといけないです。
public void setterXxxx(val) {
old = getVal();
this.val = val;
firePropertyChange("val", old, getVal());
}
old = getVal();
this.val = val;
firePropertyChange("val", old, getVal());
}
以下、大雑把なコード
package r;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.beans.AbstractBean;
public class A extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField jTextField = null;
private JCheckBox jCheckBox = null;
private JButton jButton = null;
public A() {
super();
initialize();
}
private void initialize() {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.gridy = 2;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridx = 0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJTextField(), gridBagConstraints);
this.add(getJCheckBox(), gridBagConstraints1);
this.add(getJButton(), gridBagConstraints2);
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
myBean.addPropertyChangeListener("isChecked",
new PropertyChangeListener() {
public void propertyChange(
PropertyChangeEvent propertychangeevent) {
jTextField.setText("old:"
+ propertychangeevent.getOldValue()
+ " new:"
+ propertychangeevent.getNewValue());
}
});
}
return jTextField;
}
private JCheckBox getJCheckBox() {
if (jCheckBox == null) {
jCheckBox = new JCheckBox();
jCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myBean.setChecked(jCheckBox.isSelected());
}
});
myBean.addPropertyChangeListener("isChecked",
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
jCheckBox.setSelected(myBean.isChecked());
}
});
}
return jCheckBox;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myBean.setChecked(myBean.isChecked() ? false : true);
}
});
}
return jButton;
}
MyBean myBean = new MyBean();
public class MyBean extends AbstractBean {
boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
boolean old = isChecked();
this.isChecked = isChecked;
firePropertyChange("isChecked", old, isChecked());
}
}
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.beans.AbstractBean;
public class A extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField jTextField = null;
private JCheckBox jCheckBox = null;
private JButton jButton = null;
public A() {
super();
initialize();
}
private void initialize() {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.gridy = 2;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridx = 0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJTextField(), gridBagConstraints);
this.add(getJCheckBox(), gridBagConstraints1);
this.add(getJButton(), gridBagConstraints2);
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
myBean.addPropertyChangeListener("isChecked",
new PropertyChangeListener() {
public void propertyChange(
PropertyChangeEvent propertychangeevent) {
jTextField.setText("old:"
+ propertychangeevent.getOldValue()
+ " new:"
+ propertychangeevent.getNewValue());
}
});
}
return jTextField;
}
private JCheckBox getJCheckBox() {
if (jCheckBox == null) {
jCheckBox = new JCheckBox();
jCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myBean.setChecked(jCheckBox.isSelected());
}
});
myBean.addPropertyChangeListener("isChecked",
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
jCheckBox.setSelected(myBean.isChecked());
}
});
}
return jCheckBox;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myBean.setChecked(myBean.isChecked() ? false : true);
}
});
}
return jButton;
}
MyBean myBean = new MyBean();
public class MyBean extends AbstractBean {
boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
boolean old = isChecked();
this.isChecked = isChecked;
firePropertyChange("isChecked", old, isChecked());
}
}
}
: