📄 jpopupmenu.java
字号:
return ((PopupMenuListener[]) listenerList.getListeners(PopupMenuListener.class)); } /** * This method calls popupMenuWillBecomeVisible() of popup menu's * PopupMenuListeners. This method is invoked just before popup menu * will appear on the screen. */ protected void firePopupMenuWillBecomeVisible() { EventListener[] ll = listenerList.getListeners(PopupMenuListener.class); for (int i = 0; i < ll.length; i++) ((PopupMenuListener) ll[i]).popupMenuWillBecomeVisible(new PopupMenuEvent(this)); } /** * This method calls popupMenuWillBecomeInvisible() of popup * menu's PopupMenuListeners. This method is invoked just before popup * menu will disappear from the screen */ protected void firePopupMenuWillBecomeInvisible() { EventListener[] ll = listenerList.getListeners(PopupMenuListener.class); for (int i = 0; i < ll.length; i++) ((PopupMenuListener) ll[i]).popupMenuWillBecomeInvisible(new PopupMenuEvent(this)); } /** * This method calls popupMenuCanceled() of popup menu's PopupMenuListeners. * This method is invoked just before popup menu is cancelled. This happens * when popup menu is closed without selecting any of its menu items. This * usually happens when the top-level window is resized or moved. */ protected void firePopupMenuCanceled() { EventListener[] ll = listenerList.getListeners(PopupMenuListener.class); for (int i = 0; i < ll.length; i++) ((PopupMenuListener) ll[i]).popupMenuCanceled(new PopupMenuEvent(this)); } /** * This methods sets popup menu's size to its' preferred size. If the * popup menu's size is previously set it will be ignored. */ public void pack() { // Hook up this call so that it gets executed on the event thread in order // to avoid synchronization problems when calling the layout manager. if (! SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater(new Runnable() { public void run() { show(); } }); } setSize(getPreferredSize()); } /** * Return visibility of the popup menu * * @return true if popup menu is visible on the screen and false otherwise. */ public boolean isVisible() { return visible; } /** * Sets visibility property of this popup menu. If the property is * set to true then popup menu will be dispayed and popup menu will * hide itself if visible property is set to false. * * @param visible true if popup menu will become visible and false otherwise. */ public void setVisible(final boolean visible) { // Hook up this call so that it gets executed on the event thread in order // to avoid synchronization problems when calling the layout manager. if (! SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater(new Runnable() { public void run() { setVisible(visible); } }); } if (visible == isVisible()) return; boolean old = isVisible(); this.visible = visible; if (old != isVisible()) { firePropertyChange("visible", old, isVisible()); if (visible) { firePopupMenuWillBecomeVisible(); PopupFactory pf = PopupFactory.getSharedInstance(); pack(); popup = pf.getPopup(invoker, this, popupLocationX, popupLocationY); popup.show(); } else { firePopupMenuWillBecomeInvisible(); popup.hide(); } } } /** * Sets location of the popup menu. * * @param x X coordinate of the popup menu's location * @param y Y coordinate of the popup menu's location */ public void setLocation(int x, int y) { popupLocationX = x; popupLocationY = y; // Handle the case when the popup is already showing. In this case we need // to fetch a new popup from PopupFactory and use this. See the general // contract of the PopupFactory. } /** * Returns popup menu's invoker. * * @return popup menu's invoker */ public Component getInvoker() { return invoker; } /** * Sets popup menu's invoker. * * @param component The new invoker of this popup menu */ public void setInvoker(Component component) { invoker = component; } /** * This method displays JPopupMenu on the screen at the specified * location. Note that x and y coordinates given to this method * should be expressed in terms of the popup menus' invoker. * * @param component Invoker for this popup menu * @param x x-coordinate of the popup menu relative to the specified invoker * @param y y-coordiate of the popup menu relative to the specified invoker */ public void show(Component component, int x, int y) { if (component.isShowing()) { setInvoker(component); Point p = new Point(x, y); SwingUtilities.convertPointToScreen(p, component); setLocation(p.x, p.y); setVisible(true); } } /** * Returns component located at the specified index in the popup menu * * @param index index of the component to return * * @return component located at the specified index in the popup menu * * @deprecated Replaced by getComponent(int) */ public Component getComponentAtIndex(int index) { return getComponent(index); } /** * Returns index of the specified component in the popup menu * * @param component Component to look for * * @return index of the specified component in the popup menu */ public int getComponentIndex(Component component) { Component[] items = getComponents(); for (int i = 0; i < items.length; i++) { if (items[i].equals(component)) return i; } return -1; } /** * Sets size of the popup * * @param size Dimensions representing new size of the popup menu */ public void setPopupSize(Dimension size) { super.setSize(size); } /** * Sets size of the popup menu * * @param width width for the new size * @param height height for the new size */ public void setPopupSize(int width, int height) { super.setSize(width, height); } /** * Selects specified component in this popup menu. * * @param selected component to select */ public void setSelected(Component selected) { int index = getComponentIndex(selected); selectionModel.setSelectedIndex(index); } /** * Checks if this popup menu paints its border. * * @return true if this popup menu paints its border and false otherwise. */ public boolean isBorderPainted() { return borderPainted; } /** * Sets if the border of the popup menu should be * painter or not. * * @param painted true if the border should be painted and false otherwise */ public void setBorderPainted(boolean painted) { borderPainted = painted; } /** * Returns margin for this popup menu. * * @return margin for this popup menu. */ public Insets getMargin() { return margin; } /** * A string that describes this JPopupMenu. Normally only used * for debugging. * * @return A string describing this JMenuItem */ protected String paramString() { StringBuffer sb = new StringBuffer(); sb.append(super.paramString()); sb.append(",label="); if (getLabel() != null) sb.append(getLabel()); sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled()); sb.append(",margin="); if (getMargin() != null) sb.append(margin); sb.append(",paintBorder=").append(isBorderPainted()); return sb.toString(); } /** * Process mouse events forwarded from MenuSelectionManager. This method * doesn't do anything. It is here to conform to the MenuElement interface. * * @param event event forwarded from MenuSelectionManager * @param path path to the menu element from which event was generated * @param manager MenuSelectionManager for the current menu hierarchy */ public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) { // Empty Implementation. This method is needed for the implementation // of MenuElement interface } /** * Process key events forwarded from MenuSelectionManager. This method * doesn't do anything. It is here to conform to the MenuElement interface. * * @param event event forwarded from MenuSelectionManager * @param path path to the menu element from which event was generated * @param manager MenuSelectionManager for the current menu hierarchy * */ public void processKeyEvent(KeyEvent event, MenuElement[] path, MenuSelectionManager manager) { // Empty Implementation. This method is needed for the implementation // of MenuElement interface } /** * Method of MenuElement Interface. It is invoked when * popupMenu's selection has changed * * @param changed true if this popupMenu is part of current menu * hierarchy and false otherwise. */ public void menuSelectionChanged(boolean changed) { if (! changed) setVisible(false); } /** * Return subcomonents of this popup menu. This method returns only * components that implement the <code>MenuElement</code> interface. * * @return array of menu items belonging to this popup menu */ public MenuElement[] getSubElements() { Component[] items = getComponents(); ArrayList subElements = new ArrayList(); for (int i = 0; i < items.length; i++) if (items[i] instanceof MenuElement) subElements.add(items[i]); return (MenuElement[]) subElements.toArray(new MenuElement[subElements.size()]); } /** * Method of the MenuElement interface. Returns reference to itself. * * @return Returns reference to itself */ public Component getComponent() { return this; } /** * Checks if observing mouse event should trigger popup * menu to show on the screen. * * @param event MouseEvent to check * * @return true if the observing mouse event is popup trigger and false otherwise */ public boolean isPopupTrigger(MouseEvent event) { return ((PopupMenuUI) getUI()).isPopupTrigger(event); } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) accessibleContext = new AccessibleJPopupMenu(); return accessibleContext; } /** * This is the separator that can be used in popup menu. */ public static class Separator extends JSeparator { public Separator() { super(); } public String getUIClassID() { return "PopupMenuSeparatorUI"; } } protected class AccessibleJPopupMenu extends AccessibleJComponent { private static final long serialVersionUID = 7423261328879849768L; protected AccessibleJPopupMenu() { // Nothing to do here. } public AccessibleRole getAccessibleRole() { return AccessibleRole.POPUP_MENU; } } /* This class resizes popup menu and repaints popup menu appropriately if one of item's action has changed */ protected class ActionChangeListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent evt) { // We used to have a revalidate() and repaint() call here. However I think // this is not needed. Instead, a new Popup has to be fetched from the // PopupFactory and used here. } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -