JGoodies Binding: Toggle Button Adapter Example : Data Binding « Swing Components « Java






JGoodies Binding: Toggle Button Adapter Example

JGoodies Binding: Toggle Button Adapter Example
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.adapter.ToggleButtonAdapter;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.beans.Model;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

public class ToggleButtonAdapterExample extends JPanel {
    public ToggleButtonAdapterExample() {
        DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p"));
        defaultFormBuilder.setDefaultDialogBorder();

        ToggleChangeListener toggleChangeListener = new ToggleChangeListener();

        BooleanBean booleanBean = new BooleanBean();

        BeanAdapter booleanBeanAdapter = new BeanAdapter(booleanBean, true);
        booleanBeanAdapter.addBeanPropertyChangeListener(toggleChangeListener);
        ValueModel booleanValueModel = booleanBeanAdapter.getValueModel("enabled");

        JCheckBox checkBox = BasicComponentFactory.createCheckBox(booleanValueModel, "Enabled");

        JToggleButton booleanToggleButton = new JToggleButton();
        booleanToggleButton.setPreferredSize(new Dimension(20, 20));
        booleanToggleButton.setModel(new ToggleButtonAdapter(booleanValueModel));

        defaultFormBuilder.append("Check Box:", checkBox);
        defaultFormBuilder.append("Toggle Button:", booleanToggleButton);

        StopAndGoBean stopAndGoBean = new StopAndGoBean();
        BeanAdapter stopAndGoBeanAdapter = new BeanAdapter(stopAndGoBean);
        stopAndGoBeanAdapter.addBeanPropertyChangeListener(toggleChangeListener);

        ValueModel stopAndGoModel = stopAndGoBeanAdapter.getValueModel("state");

        JToggleButton stopAndGoToggleButton = new JToggleButton();
        stopAndGoToggleButton.setPreferredSize(new Dimension(20, 20));
        stopAndGoToggleButton.setModel(new ToggleButtonAdapter(stopAndGoModel, "stop", "go"));

        defaultFormBuilder.append("Stop/Go Button:", stopAndGoToggleButton);

        add(defaultFormBuilder.getPanel());
    }

    private class ToggleChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            JOptionPane.showMessageDialog(null, "Property " + evt.getPropertyName() + " was changed to " + evt.getNewValue());
        }
    }

    public class BooleanBean extends Model {
        public final static String ENABLED_PROPERTY = "enabled";
        private Boolean enabled = Boolean.TRUE;

        public Boolean getEnabled() {
            return enabled;
        }

        public void setEnabled(Boolean enabled) {
            Boolean oldValue = this.enabled;
            this.enabled = enabled;
            firePropertyChange(ENABLED_PROPERTY, oldValue, this.enabled);
        }
    }

    public class StopAndGoBean extends Model {
        public final static String STATE_PROPERTY = "state";
        private String state = "stop";

        public String getState() {
            return state;
        }

        public void setState(String state) {
            String oldState = this.state;
            this.state = state;
            firePropertyChange(STATE_PROPERTY, oldState, this.state);
        }
    }


    public static void main(String[] a){
      JFrame f = new JFrame("ToggleButtonAdapter Example");
      f.setDefaultCloseOperation(2);
      f.add(new ToggleButtonAdapterExample());
      f.pack();
      f.setVisible(true);
    }
}

           
       








jgoodiesDataBinding.zip( 254 k)

Related examples in the same category

1.Demonstrates three different styles when to commit changesDemonstrates three different styles when to commit changes
2.Builds an editor with components bound to the domain object propertiesBuilds an editor with components bound to the domain object properties
3.Using buffered adapting ValueModels created by a PresentationModelUsing buffered adapting ValueModels created by a PresentationModel
4.Builds an editor that copies data from the domain back and forthBuilds an editor that copies data from the domain back and forth
5.JGoodies Binding: Selection In List Bean Channel ExampleJGoodies Binding: Selection In List Bean Channel Example
6.JGoodies Binding: Selection In List ExampleJGoodies Binding: Selection In List Example
7.JGoodies Binding: Selection In List Model ExampleJGoodies Binding: Selection In List Model Example
8.JGoodies Binding: Value Holder ExampleJGoodies Binding: Value Holder Example
9.JGoodies Binding: Abstract Table Model ExampleJGoodies Binding: Abstract Table Model Example
10.JGoodies Binding: Basic Component Factory ExampleJGoodies Binding: Basic Component Factory Example
11.JGoodies Binding: Bean Adapter ExampleJGoodies Binding: Bean Adapter Example
12.JGoodies Binding: Bounded Range Adapter ExampleJGoodies Binding: Bounded Range Adapter Example
13.JGoodies Binding: Buffering Presentation Model ExampleJGoodies Binding: Buffering Presentation Model Example
14.JGoodies Binding: ComboBox Adapter ExampleJGoodies Binding: ComboBox Adapter Example
15.JGoodies Binding: Converter Factory ExampleJGoodies Binding: Converter Factory Example
16.JGoodies Binding: Feed Bean Example 1JGoodies Binding: Feed Bean Example 1
17.JGoodies Binding: Feed Bean Example 2JGoodies Binding: Feed Bean Example 2
18.JGoodies Binding: Feed Bean Example 3JGoodies Binding: Feed Bean Example 3
19.JGoodies Binding: Presentation Bean Channel ExampleJGoodies Binding: Presentation Bean Channel Example
20.JGoodies Binding: Presentation Bean Property Change Listener ExampleJGoodies Binding: Presentation Bean Property Change Listener Example
21.JGoodies Binding: Presentation Model Property Change ExampleJGoodies Binding: Presentation Model Property Change Example
22.JGoodies Binding: Property Adapter Example 1JGoodies Binding: Property Adapter Example 1
23.JGoodies Binding: Property Adapter Example 2JGoodies Binding: Property Adapter Example 2
24.JGoodies Binding: Property Adapter Example 3JGoodies Binding: Property Adapter Example 3
25.JGoodies Binding: Property Connector ExampleJGoodies Binding: Property Connector Example
26.JGoodies Binding: Radio Button Adapter ExampleJGoodies Binding: Radio Button Adapter Example
27.Swingx: Swing Data BindingSwingx: Swing Data Binding