最近在做一個Java Swing的小程序,試了一下Bean Binding,需要用到的lib有兩個,appframework-1.03.jar和beanbind-1.2.1.jar. 完整代碼如下:
Entrance.java
package main;
import swing.MainFrame;
public class Entrance {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame mf = new MainFrame();
mf.setup();
}
}
Entity.java
package bean;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Entity {
private String inputtext;
private PropertyChangeSupport changeSupport=new PropertyChangeSupport(this);
public String getInputtext() {
return inputtext;
}
public void setInputtext(String inputtext) {
String oldText=this.inputtext;
this.inputtext = inputtext;
changeSupport.firePropertyChange("inputtext", oldText, inputtext);
}
public void addPropertyChangeListener(PropertyChangeListener listener){
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener){
changeSupport.removePropertyChangeListener(listener);
}
}
MainFrame.java
package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class MainFrame {
private JFrame frame;
public void setup(){
frame=new JFrame();
frame.setPreferredSize(new Dimension(600,400));
frame.getContentPane().add(new MainWindow().createPanel(),BorderLayout.CENTER);
frame.setVisible(true);
frame.addWindowListener(new windowEventHandler());
}
class windowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.out.println("Exit");
System.exit(0);
}
}
}
MainWindow.java
package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import bean.Entity;
public class MainWindow implements ActionListener {
private JPanel mainPanel;
private JTextField textField;
private JButton button;
public final static Entity entity = new Entity();
public JPanel createPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setPreferredSize(new Dimension(300, 200));
textField = new JTextField();
BeanProperty<Entity, String> inputtextProperty = BeanProperty
.create("inputtext");
BeanProperty<JTextField, String> textProperty = BeanProperty
.create("text");
/* From java bean to swing component */
AutoBinding<Entity, String, JTextField, String>
binding1=Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
entity, inputtextProperty, textField, textProperty);
/* From swing component to java bean */
//AutoBinding<MyTextField, String, Entity, String> binding2 = Bindings
// .createAutoBinding(UpdateStrategy.READ_WRITE, textField,
// textProperty, entity, inputtextProperty);
binding1.bind();
//binding2.bind();
mainPanel.add(textField, BorderLayout.CENTER);
button = new JButton("Change Text");
button.addActionListener(this);
mainPanel.add(button, BorderLayout.SOUTH);
return mainPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == button) {
/* From java bean to swing component */
entity.setInputtext("Hello");
System.out.println("entity value: "+entity.getInputtext());
System.out.println("component value: "+textField.getText());
mainPanel.updateUI();
/* From swing component to java bean */
//System.out.println("component value: " + textField.getText());
//System.out.println("entity value: " + entity.getInputtext());
}
}
}
說明:
上述代碼只實現了修改java bean中attribute中的值能夠自動在swing component中通過beanbinding顯示出來,反過來並不成立,如果有大蝦知道如何通過修改component的屬性值從而自動修改bean中的屬性值,還請指教。
Java Swing中的透明窗體(Windows中消息提示框的制作) http://www.linuxidc.com/Linux/2014-04/99622.htm
用Java Swing編寫簡單的測試小工具界面(源碼) http://www.linuxidc.com/Linux/2012-11/73566.htm
Java 學習之 Swing 編程 http://www.linuxidc.com/Linux/2014-05/101049.htm