Java examples for Swing:Action
create JMenus from Action array
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 JMenu[] createMenus(String[] title, Action[][] actions) { if (title == null || actions == null) { throw new NullPointerException(); } else if (title.length != actions.length) { throw new IllegalArgumentException( "title and action arrays not equal length: " + title.length + ", " + actions.length); }/*from w w w . j a v a 2 s .c om*/ JMenu[] menu = new JMenu[title.length]; for (int i = 0; i < title.length; i++) { menu[i] = createMenu(title[i], actions[i]); } return menu; } public static JMenu createMenu(String title, Object usingObject) { if (usingObject == null) { return new JMenu(); } title = title == null ? "" : title; ActionInfo ai = ActionIntrospector.getActionInfo(usingObject .getClass()); Action[] actions = ai.getActions(usingObject); return createMenu(title, actions); } public static JMenu createMenu(String title, Object usingObject, String[] justThese) { Action[] actions = ActionIntrospector.getActions(usingObject, justThese); return createMenu(title, actions); } public static JMenu createMenu(String title, Object[] usingObject) { if (usingObject == null) { return null; } title = title != null ? title : ""; JMenu menu = new JMenu(title); for (int i = 0; i < usingObject.length; i++) { if (usingObject[i] == null) { continue; } Action[] actions = ActionIntrospector .getActions(usingObject[i]); for (int j = 0; j < actions.length; j++) { menu.add(createMenuItem(actions[j])); } if (i < usingObject.length - 1) { menu.addSeparator(); } } return menu; } public static JMenu createMenu(String title, Action[] actions) { if (actions == null) { return null; } title = title != null ? title : ""; JMenu menu = new JMenu(title); for (int i = 0; i < actions.length; i++) { menu.add(createMenuItem(actions[i])); } return menu; } public static JMenuItem createMenuItem(Action action) { JMenuItem menuItem = new JMenuItem(action); JButtonListener listener = new JButtonListener(menuItem); action.addPropertyChangeListener(listener); configureButton(action, listener); return menuItem; } 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()))); } } } }