Swing中使用Bean Binding实例

最近在做一个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();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1c854fefa4b2323998d20012c7ed68a3.html