/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import com.jgoodies.binding.adapter.RadioButtonAdapter;
import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class RadioButtonAdapterExample extends JPanel {
private JLabel radioLabel;
public RadioButtonAdapterExample() {
PanelBuilder panelBuilder = new PanelBuilder(new FormLayout("p, 2dlu, p, 2dlu, p, 2dlu, p", "p"));
panelBuilder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
ValueHolder selectionModel = new ValueHolder();
JRadioButton radioButton1 = new JRadioButton("Red");
RadioButtonAdapter radioButtonAdapter1 = new RadioButtonAdapter(selectionModel, Color.red);
radioButton1.setModel(radioButtonAdapter1);
JRadioButton radioButton2 = new JRadioButton("Blue");
RadioButtonAdapter radioButtonAdapter2 = new RadioButtonAdapter(selectionModel, Color.blue);
radioButton2.setModel(radioButtonAdapter2);
JRadioButton radioButton3 = new JRadioButton("Green");
RadioButtonAdapter radioButtonAdapter3 = new RadioButtonAdapter(selectionModel, Color.green);
radioButton3.setModel(radioButtonAdapter3);
this.radioLabel = panelBuilder.addLabel("Radio Buttons:", cc.xy(1, 1));
this.radioLabel.setFont(this.radioLabel.getFont().deriveFont(Font.BOLD));
panelBuilder.add(radioButton1, cc.xy(3, 1));
panelBuilder.add(radioButton2, cc.xy(5, 1));
panelBuilder.add(radioButton3, cc.xy(7, 1));
selectionModel.setValue("blue");
selectionModel.addPropertyChangeListener(new RadioChangeListener());
add(panelBuilder.getPanel());
}
private class RadioChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
radioLabel.setForeground((Color) evt.getNewValue());
}
}
public static void main(String[] a){
JFrame f = new JFrame("Radio Button Adapter Example");
f.setDefaultCloseOperation(2);
f.add(new RadioButtonAdapterExample());
f.pack();
f.setVisible(true);
}
}
jgoodiesDataBinding.zip( 254 k)