bind Swing Action To Properties - Java Swing

Java examples for Swing:Action

Description

bind Swing Action To Properties

Demo Code


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class Main{
    public static void bindActionToProperties(Action action, Object object) {
        if (object == null) {
            return;
        }/*from w  w w  .  ja va 2s. c o m*/
        JButtonListener listener = new JButtonListener(object);
        configureButton(action, listener);
    }
    public static void configureButton(Action action,
            PropertyChangeListener listener) {
        if (action == null || listener == null) {
            return;
        }
        action.addPropertyChangeListener(listener);
        action.putValue("listener", listener);
        if (action instanceof AbstractAction) {
            AbstractAction abstractAction = (AbstractAction) action;
            Object[] key = abstractAction.getKeys();
            for (int i = 0; i < key.length; i++) {
                listener.propertyChange(new PropertyChangeEvent(action,
                        key[i].toString(), null, abstractAction
                                .getValue(key[i].toString())));
            }
        }
    }
}

Related Tutorials