下面列出了javax.swing.JPopupMenu#addPopupMenuListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
JPopupMenu createPopupMenu(Iterator<DockingActionIf> localActions, ActionContext context) {
if (localActions == null) {
localActions = IteratorUtils.emptyIterator();
}
MenuHandler popupMenuHandler = new PopupMenuHandler(windowManager, context);
MenuManager menuMgr =
new MenuManager("Popup", '\0', null, true, popupMenuHandler, menuGroupMap);
populatePopupMenuActions(localActions, context, menuMgr);
if (menuMgr.isEmpty()) {
return null;
}
// Popup menu if items are available
JPopupMenu popupMenu = menuMgr.getPopupMenu();
popupMenu.addPopupMenuListener(popupMenuHandler);
return popupMenu;
}
private void doSelect(JComponent owner) {
invokingComponent = owner;
invokingComponent.addMouseListener(this);
invokingComponent.addMouseMotionListener(this);
pTable.addMouseListener(this);
pTable.addMouseMotionListener(this);
pTable.getSelectionModel().addListSelectionListener( this );
displayer.getModel().addComplexListDataListener( this );
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
popup = new JPopupMenu();
popup.setBorderPainted( false );
popup.setBorder( BorderFactory.createEmptyBorder() );
popup.add( pTable );
popup.pack();
int locationX = x - (int) pTable.getPreferredSize().getWidth();
int locationY = y + 1;
popup.setLocation( locationX, locationY );
popup.setInvoker( invokingComponent );
popup.addPopupMenuListener( this );
popup.setVisible( true );
shown = true;
invocationTime = System.currentTimeMillis();
}
private void doSelect(JComponent owner) {
invokingComponent = owner;
invokingComponent.addMouseListener(this);
invokingComponent.addMouseMotionListener(this);
pTable.addMouseListener(this);
pTable.addMouseMotionListener(this);
pTable.getSelectionModel().addListSelectionListener( this );
controller.getTabModel().addComplexListDataListener( this );
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
popup = new JPopupMenu();
popup.setBorderPainted( false );
popup.setBorder( BorderFactory.createEmptyBorder() );
popup.add( pTable );
popup.pack();
int locationX = x - (int) pTable.getPreferredSize().getWidth();
int locationY = y + 1;
popup.setLocation( locationX, locationY );
popup.setInvoker( invokingComponent );
popup.addPopupMenuListener( this );
popup.setVisible( true );
shown = true;
invocationTime = System.currentTimeMillis();
}
/**
* Constructs a <code>MenuScroller</code> that scrolls a popup menu with the
* specified number of items to display in the scrolling region, the
* specified scrolling interval, and the specified numbers of items fixed at
* the top and bottom of the popup menu.
*
* @param menu the popup menu
* @param scrollCount the number of items to display in the scrolling
* portion
* @param interval the scroll interval, in milliseconds
* @param topFixedCount the number of items to fix at the top. May be 0
* @param bottomFixedCount the number of items to fix at the bottom. May be
* 0
* @throws IllegalArgumentException if scrollCount or interval is 0 or
* negative or if topFixedCount or bottomFixedCount is negative
*/
public MenuScroller(JPopupMenu menu, int scrollCount, int interval,
int topFixedCount, int bottomFixedCount) {
if (scrollCount <= 0 || interval <= 0) {
throw new IllegalArgumentException("scrollCount and interval must be greater than 0");
}
if (topFixedCount < 0 || bottomFixedCount < 0) {
throw new IllegalArgumentException("topFixedCount and bottomFixedCount cannot be negative");
}
upItem = new MenuScrollItem(MenuIcon.UP, -1);
downItem = new MenuScrollItem(MenuIcon.DOWN, +1);
setScrollCount(scrollCount);
setInterval(interval);
setTopFixedCount(topFixedCount);
setBottomFixedCount(bottomFixedCount);
this.menu = menu;
menu.addPopupMenuListener(menuListener);
}
@Override
public void loggedActionPerformed(ActionEvent ae) {
JPopupMenu popup = getPopupMenu();
popup.addPopupMenuListener(popupMenuListener);
int popupPrefHeight = (int) popup.getPreferredSize().getHeight();
int buttonY = mainButton.getLocationOnScreen().y;
boolean showOnTop = false;
GraphicsConfiguration graphicsConf = ApplicationFrame.getApplicationFrame().getGraphicsConfiguration();
if (graphicsConf != null) {
int windowHeight = (int) graphicsConf.getBounds().getHeight();
showOnTop = buttonY + mainButton.getHeight() + popupPrefHeight > windowHeight;
}
if (showOnTop) {
popup.show(mainButton, 0, -popupPrefHeight);
} else {
popup.show(mainButton, 0, mainButton.getHeight());
}
}
public JDropDownButton(final String text, final Icon icon, final int popupHorizontalAlignment, final int popupVerticalAlignment) {
super(text, icon);
if (popupHorizontalAlignment != LEFT && popupHorizontalAlignment != RIGHT && popupHorizontalAlignment != CENTER) {
throw new IllegalArgumentException("Must be SwingConstants.RIGHT, SwingConstants.LEFT, or SwingConstants.CENTER");
}
if (popupVerticalAlignment != TOP && popupVerticalAlignment != BOTTOM && popupVerticalAlignment != CENTER) {
throw new IllegalArgumentException("Must be SwingConstants.TOP, SwingConstants.BOTTOM, or SwingConstants.CENTER");
}
ListenerClass listener = new ListenerClass();
this.popupHorizontalAlignment = popupHorizontalAlignment;
this.popupVerticalAlignment = popupVerticalAlignment;
this.setText(text);
this.addMouseListener(listener);
this.addKeyListener(listener);
jPopupMenu = new JPopupMenu();
jPopupMenu.addPopupMenuListener(listener);
menuScroller = new MenuScroller(jPopupMenu);
}
/**
* Constructs a <code>MenuScroller</code> that scrolls a popup menu with the
* specified number of items to display in the scrolling region, the
* specified scrolling interval, and the specified numbers of items fixed at
* the top and bottom of the popup menu.
*
* @param menu the popup menu
* @param scrollCount the number of items to display in the scrolling portion
* @param interval the scroll interval, in milliseconds
* @param topFixedCount the number of items to fix at the top. May be 0
* @param bottomFixedCount the number of items to fix at the bottom. May be 0
* @throws IllegalArgumentException if scrollCount or interval is 0 or
* negative or if topFixedCount or bottomFixedCount is negative
*/
public MenuScroller(JPopupMenu menu, int scrollCount, int interval,
int topFixedCount, int bottomFixedCount) {
if (scrollCount <= 0 || interval <= 0) {
throw new IllegalArgumentException("scrollCount and interval must be greater than 0");
}
if (topFixedCount < 0 || bottomFixedCount < 0) {
throw new IllegalArgumentException("topFixedCount and bottomFixedCount cannot be negative");
}
upItem = new MenuScrollItem(MenuIcon.UP, -1);
downItem = new MenuScrollItem(MenuIcon.DOWN, +1);
setScrollCount(scrollCount);
setInterval(interval);
setTopFixedCount(topFixedCount);
setBottomFixedCount(bottomFixedCount);
this.menu = menu;
menu.addPopupMenuListener(menuListener);
}
private void fillSubmenu(JPopupMenu pop) {
if (lastPopup == null) {
pop.addPopupMenuListener(this);
lastPopup = pop;
removeAll();
Iterator it = generate(toolsAction, false).iterator();
while (it.hasNext()) {
java.awt.Component item = (java.awt.Component) it.next();
if (item == null) {
addSeparator();
} else {
add(item);
}
}
// also work with empty element
if (getMenuComponentCount() == 0) {
JMenuItem empty = new JMenuItem(NbBundle.getMessage(ToolsAction.class, "CTL_EmptySubMenu"));
empty.setEnabled(false);
add(empty);
}
}
}
@Override
public Component getToolbarPresenter() {
JPopupMenu menu = new JPopupMenu();
JButton button = DropDownButtonFactory.createDropDownButton(
new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)), menu);
final JMenuItem item = new JMenuItem(org.openide.awt.Actions.cutAmpersand((String) getValue("menuText")));
item.setEnabled(isEnabled());
addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if ("enabled".equals(propName)) {
item.setEnabled((Boolean) evt.getNewValue());
} else if ("menuText".equals(propName)) {
item.setText(org.openide.awt.Actions.cutAmpersand((String) evt.getNewValue()));
}
}
});
menu.add(item);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainProjectActionWithHistory.this.actionPerformed(e);
}
});
org.openide.awt.Actions.connect(button, this);
menu.addPopupMenuListener(this);
return button;
}
public void showPopupMenu(JPopupMenu menu, int x, int y) {
double zoom = getZoomFactor();
if (zoom != 1.0) {
x = (int) Math.round(x * zoom);
y = (int) Math.round(y * zoom);
}
myListener.menu_on = true;
menu.addPopupMenuListener(myListener);
menu.show(this, x, y);
}
@Override
public void loggedActionPerformed(ActionEvent ae) {
JPopupMenu popup = getPopupMenu();
popup.addPopupMenuListener(popupMenuListener);
popup.show(mainButton,
isRightAlign() ? -popup.getPreferredSize().width + mainButton.getWidth() + arrowButton.getWidth() : 0,
mainButton.getHeight());
}
public void setJPopupMenu(JPopupMenu menu) {
if (this.menu != null) {
this.menu.removePopupMenuListener(popupListener);
}
this.menu = menu;
menu.addPopupMenuListener(popupListener);
}
public RecentArchivesButton() {
super();
setIcon(GuiMode.getTheme().getRecentIcon());
setToolTipText("History");
popup = new JPopupMenu();
theme.applyTo(popup);
popup.setLayout(new BoxLayout(popup, BoxLayout.Y_AXIS));
popup.addPopupMenuListener(new PopupPrintListener());
buildPopup();
setBorderPainted(false);
setFocusPainted(true);
addMouseListener(new MousePopupListener());
}
/**
* Construct and show popup menu.
*
* @param component Component.
* @param link Selected page.
* @param x Position.
* @param y Position.
*/
private void showPopup(Component component, Page link, int x, int y) {
// Menu name
JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem(link.getTitle());
menuItem.setEnabled(false);
popup.add(menuItem);
// Create sub menus
createPopup(popup, link);
popup.show(component, x, y);
popup.addPopupMenuListener(this);
}
@Override public Component getToolbarPresenter() {
JPopupMenu menu = new JPopupMenu();
JButton button = DropDownButtonFactory.createDropDownButton(
new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)), menu);
final JMenuItem item = new JMenuItem(Actions.cutAmpersand((String) delegate.getValue("menuText")));
item.setEnabled(delegate.isEnabled());
delegate.addPropertyChangeListener(new PropertyChangeListener() {
@Override public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if ("enabled".equals(propName)) {
item.setEnabled((Boolean)evt.getNewValue());
} else if ("menuText".equals(propName)) {
item.setText(Actions.cutAmpersand((String) evt.getNewValue()));
} else if ("selectedProjects".equals(propName)) {
Project[] projects = (Project[]) evt.getNewValue();
if (projects.length == 1) {
debugHistorySupport.setSelectedProject(projects[0].getProjectDirectory());
} else {
debugHistorySupport.setSelectedProject(null);
}
}
}
});
menu.add(item);
item.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
DebugMainProjectAction.this.actionPerformed(e);
}
});
try {
Action ca = Actions.forID("Debug", "org.netbeans.modules.debugger.ui.actions.ConnectAction");
JMenuItem item2 = new JMenuItem(Actions.cutAmpersand((String) ca.getValue(NAME)));
Actions.connect(item2, ca);
menu.add(item2);
} catch (Exception nsee) {
Exceptions.printStackTrace(nsee);
}
menu.addPopupMenuListener(this);
Actions.connect(button, this);
return button;
}
public void actionPerformed(ActionEvent event) {
JPopupMenu popup = getPopupMenu();
popup.addPopupMenuListener(this);
popup.show(mainButton, 0, mainButton.getHeight());
}