configure Swing Button via Action - Java Swing

Java examples for Swing:Action

Description

configure Swing Button via Action

Demo Code


//package com.java2s;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.Action;

public class Main {
    public static void configureButton(Action action,
            PropertyChangeListener listener) {
        if (action == null || listener == null) {
            return;
        }// ww w . ja v  a2 s .  co m
        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