Databinding

Mogwai DataBinding is a framework to bind Java Beans / Pojo's to Swing visual components. It extends the Swing MVC architecture and supports a Mircosoft Document/View style handlingExample code ( also included in the package ) :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.mogwai.binding.*;
import de.mogwai.binding.adaptor.ComboboxModelAdaptor;
import de.mogwai.binding.adaptor.RadioButtonAdaptor;

/**
* @author msertic
*/
public class BindingTest {

public static class MyBean {

}

public static void main(String args[]) throws IllegalAccessException,

InvocationTargetException, NoSuchMethodException {

// Setup the view
JFrame frame=new JFrame("Test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(640,480);

CellConstraints cons=new CellConstraints();

FormLayout layout=new FormLayout("90dlu,80dlu:grow",

"2dlu,p,2dlu,p,2dlu,p,2dlu,p,2dlu,p,2dlu,p,p,p,2dlu");

JPanel content=new JPanel();
content.setLayout(layout);

frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(content,BorderLayout.CENTER);

JLabel label1=new JLabel("JTextField with value");
JTextField textfield1=new JTextField();

content.add(label1,cons.xy(1,2));
content.add(textfield1,cons.xy(2,2));

JLabel label2=new JLabel("JTextField without value");
JTextField textfield2=new JTextField();

content.add(label2,cons.xy(1,4));
content.add(textfield2,cons.xy(2,4));

JLabel label3=new JLabel("JCheckBox");
JCheckBox box3=new JCheckBox();

content.add(label3,cons.xy(1,6));
content.add(box3,cons.xy(2,6));

JLabel label4=new JLabel("JCheckBox bound to Vector");
JComboBox combo1=new JComboBox();

content.add(label4,cons.xy(1,8));
content.add(combo1,cons.xy(2,8));

JLabel label5=new JLabel("JCheckBox bound to Object[]");
JComboBox combo2=new JComboBox();

content.add(label5,cons.xy(1,10));
content.add(combo2,cons.xy(2,10));

JLabel label6=new JLabel("JRadioButton");
JRadioButton radio1=new JRadioButton("Radio 1");
JRadioButton radio2=new JRadioButton("Radio 2");
JRadioButton radio3=new JRadioButton("Radio 3");

content.add(label6,cons.xy(1,12));
content.add(radio1,cons.xy(2,12));
content.add(radio2,cons.xy(2,13));
content.add(radio3,cons.xy(2,14));


//
//
// Now, here comes the tricky part !
//
//

// Setup the model
Model model=new Model();
model.setString1("Wutzpu");
model.setBoolean(true);
model.setSelected("2");

// Setup the binding
final BindingInfo binding=new BindingInfo(Model.class);
binding.addBinding("string1",textfield1);
binding.addBinding("string2",textfield2);
binding.addBinding("boolean",box3);
binding.addBinding("type",combo1);
binding.addBinding("entries",new ComboboxModelAdaptor(combo1));
binding.addBinding("entriesArray",new ComboboxModelAdaptor(combo2));

RadioButtonAdaptor adaptor=new RadioButtonAdaptor();
adaptor.addMapping("1",radio1);
adaptor.addMapping("2",radio2);
adaptor.addMapping("3",radio3);
binding.addBinding("selected",adaptor);

binding.setDefaultModel(model);

// Initialize the view !
// This also forces the collection to model mapping to be initialized !
binding.model2view();

// Event listener
JButton button=new JButton("View 2 Model");

//
// This is an example event listener !
//
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

// Transfer the data to the model
binding.view2model();

// Get the model
Model model=(Model)binding.getDefaultModel();

// Modify the model
String test=model.getType()+" "+model.getSelected();
model.setString2(test);

// Transfer the model to the view
binding.model2view();
}
});
frame.getContentPane().add(button,BorderLayout.NORTH);

frame.show();

}

}

Download latest release at SourceForge

Download


Project Homepage:
http://sourceforge.net/projects/mogwai

Examples and Documentation:
http://www.mirkosertic.de/doku.php/javastuff/swingdatabinding
SourceForge.net Logo

Support This Project
Validate XHTML & CSS