下面列出了javax.swing.JMenu#addMenuListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Report the concrete UI menu of all plugins
*
* @param menu a preallocated menu instance, or null
* @return the populated menu entity
*/
public JMenu getMenu (JMenu menu)
{
if (menu == null) {
menu = new SeparableMenu();
}
for (Plugin plugin : plugins) {
menu.add(new JMenuItem(new PluginAction(plugin)));
}
// Listener to modify attributes on-the-fly
menu.addMenuListener(new MyMenuListener());
this.menu = menu;
return menu;
}
/**
* Report the concrete UI menu of all plugins
*
* @param menu a preallocated menu instance, or null
* @return the populated menu entity
*/
public JMenu getMenu (JMenu menu)
{
if (menu == null) {
menu = new SeparableMenu();
}
for (Plugin plugin : map.values()) {
menu.add(new JMenuItem(new PluginAction(plugin)));
}
// Listener to modify attributes on-the-fly
menu.addMenuListener(new MyMenuListener());
this.menu = menu;
return menu;
}
public static void build(JPopupMenu menu, Figure figure, FigureWidget figureWidget, boolean successors, DiagramScene diagramScene) {
Set<Figure> set = figure.getPredecessorSet();
if (successors) {
set = figure.getSuccessorSet();
}
boolean first = true;
for (Figure f : set) {
if (f == figure) {
continue;
}
if (first) {
first = false;
} else {
menu.addSeparator();
}
Action go = diagramScene.createGotoAction(f);
menu.add(go);
JMenu preds = new JMenu("Nodes Above");
preds.addMenuListener(figureWidget.new NeighborMenuListener(preds, f, false));
menu.add(preds);
JMenu succs = new JMenu("Nodes Below");
succs.addMenuListener(figureWidget.new NeighborMenuListener(succs, f, true));
menu.add(succs);
}
if (figure.getPredecessorSet().isEmpty() && figure.getSuccessorSet().isEmpty()) {
menu.add("(none)");
}
}
/**
* Feed a menu with the dynamic content of this NameSet.
*
* @param menu the menu to be fed, if null it is allocated by this method
* @param itemListener the listener to be called on item selection
* @return the menu properly dynamized
*/
public JMenu feedMenu (JMenu menu,
final ActionListener itemListener)
{
final JMenu finalMenu = (menu != null) ? menu : new JMenu(setName);
MenuListener menuListener = new AbstractMenuListener()
{
@Override
public void menuSelected (MenuEvent e)
{
// Clean up the whole menu
finalMenu.removeAll();
// Rebuild the whole list of menu items on the fly
synchronized (NameSet.this) {
for (String f : names) {
JMenuItem menuItem = new JMenuItem(f);
menuItem.addActionListener(itemListener);
finalMenu.add(menuItem);
}
}
}
};
// Listener to menu selection, to modify content on-the-fly
finalMenu.addMenuListener(menuListener);
return finalMenu;
}
/**
* Create the dynamic menu.
*
* @param menuLabel the label to be used for the menu
*/
public DynamicMenu (String menuLabel)
{
menu = new JMenu(menuLabel);
// Listener to menu selection, to modify content on-the-fly
menu.addMenuListener(listener);
}
/**
* Creates a new DynamicMenu object.
*
* @param action related action
*/
public DynamicMenu (Action action)
{
menu = new JMenu(action);
// Listener to menu selection, to modify content on-the-fly
menu.addMenuListener(listener);
}
private void attachToMenu (JMenu menu) {
menu.addContainerListener(getMenuListener());
menu.addComponentListener(getMenuListener());
menu.addMenuListener(getMenuListener());
}
/** @param menu The {@link JMenu} to add. */
public static void add(JMenu menu) {
menu.addMenuListener(INSTANCE);
}