Method from javax.swing.JPopupMenu Detail: |
public JMenuItem add(JMenuItem menuItem) {
super.add(menuItem);
return menuItem;
}
Appends the specified menu item to the end of this menu. |
public JMenuItem add(String s) {
return add(new JMenuItem(s));
}
Creates a new menu item with the specified text and appends
it to the end of this menu. |
public JMenuItem add(Action a) {
JMenuItem mi = createActionComponent(a);
mi.setAction(a);
add(mi);
return mi;
}
Appends a new menu item to the end of the menu which
dispatches the specified Action object. |
public void addMenuKeyListener(MenuKeyListener l) {
listenerList.add(MenuKeyListener.class, l);
}
Adds a MenuKeyListener to the popup menu. |
public void addPopupMenuListener(PopupMenuListener l) {
listenerList.add(PopupMenuListener.class,l);
}
Adds a PopupMenu listener. |
public void addSeparator() {
add( new JPopupMenu.Separator() );
}
Appends a new separator at the end of the menu. |
Point adjustPopupLocationToFitScreen(int xPosition,
int yPosition) {
Point popupLocation = new Point(xPosition, yPosition);
if(popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) {
return popupLocation;
}
// Get screen bounds
Rectangle scrBounds;
GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
Toolkit toolkit = Toolkit.getDefaultToolkit();
if(gc != null) {
// If we have GraphicsConfiguration use it to get screen bounds
scrBounds = gc.getBounds();
} else {
// If we don't have GraphicsConfiguration use primary screen
scrBounds = new Rectangle(toolkit.getScreenSize());
}
// Calculate the screen size that popup should fit
Dimension popupSize = JPopupMenu.this.getPreferredSize();
long popupRightX = (long)popupLocation.x + (long)popupSize.width;
long popupBottomY = (long)popupLocation.y + (long)popupSize.height;
int scrWidth = scrBounds.width;
int scrHeight = scrBounds.height;
if (!canPopupOverlapTaskBar()) {
// Insets include the task bar. Take them into account.
Insets scrInsets = toolkit.getScreenInsets(gc);
scrBounds.x += scrInsets.left;
scrBounds.y += scrInsets.top;
scrWidth -= scrInsets.left + scrInsets.right;
scrHeight -= scrInsets.top + scrInsets.bottom;
}
int scrRightX = scrBounds.x + scrWidth;
int scrBottomY = scrBounds.y + scrHeight;
// Ensure that popup menu fits the screen
if (popupRightX > (long)scrRightX) {
popupLocation.x = scrRightX - popupSize.width;
if( popupLocation.x < scrBounds.x ) {
popupLocation.x = scrBounds.x ;
}
}
if (popupBottomY > (long)scrBottomY) {
popupLocation.y = scrBottomY - popupSize.height;
if( popupLocation.y < scrBounds.y ) {
popupLocation.y = scrBounds.y;
}
}
return popupLocation;
}
Returns an point which has been adjusted to take into account of the
desktop bounds, taskbar and multi-monitor configuration.
This adustment may be cancelled by invoking the application with
-Djavax.swing.adjustPopupLocationToFit=false |
boolean alwaysOnTop() {
return true;
}
Always returns true since popups, by definition, should always
be on top of all other windows. |
static boolean canPopupOverlapTaskBar() {
boolean result = true;
try {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(
SecurityConstants.AWT.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
}
} catch (SecurityException se) {
// There is no permission to show popups over the task bar
result = false;
}
return result;
}
Checks that there are enough security permissions
to make popup "always on top", which allows to show it above the task bar. |
protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
return b.createActionPropertyChangeListener0(b.getAction());
}
Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action occur. |
protected JMenuItem createActionComponent(Action a) {
JMenuItem mi = new JMenuItem() {
protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
PropertyChangeListener pcl = createActionChangeListener(this);
if (pcl == null) {
pcl = super.createActionPropertyChangeListener(a);
}
return pcl;
}
};
mi.setHorizontalTextPosition(JButton.TRAILING);
mi.setVerticalTextPosition(JButton.CENTER);
return mi;
}
Factory method which creates the JMenuItem for
Actions added to the JPopupMenu . |
protected void firePopupMenuCanceled() {
Object[] listeners = listenerList.getListenerList();
PopupMenuEvent e=null;
for (int i = listeners.length-2; i >=0; i-=2) {
if (listeners[i]==PopupMenuListener.class) {
if (e == null)
e = new PopupMenuEvent(this);
((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
}
}
}
Notifies PopupMenuListeners that this popup menu is
cancelled. |
protected void firePopupMenuWillBecomeInvisible() {
Object[] listeners = listenerList.getListenerList();
PopupMenuEvent e=null;
for (int i = listeners.length-2; i >=0; i-=2) {
if (listeners[i]==PopupMenuListener.class) {
if (e == null)
e = new PopupMenuEvent(this);
((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
}
}
}
Notifies PopupMenuListener s that this popup menu will
become invisible. |
protected void firePopupMenuWillBecomeVisible() {
Object[] listeners = listenerList.getListenerList();
PopupMenuEvent e=null;
for (int i = listeners.length-2; i >=0; i-=2) {
if (listeners[i]==PopupMenuListener.class) {
if (e == null)
e = new PopupMenuEvent(this);
((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
}
}
}
Notifies PopupMenuListener s that this popup menu will
become visible. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJPopupMenu();
}
return accessibleContext;
}
Gets the AccessibleContext associated with this JPopupMenu.
For JPopupMenus, the AccessibleContext takes the form of an
AccessibleJPopupMenu.
A new AccessibleJPopupMenu instance is created if necessary. |
public Component getComponent() {
return this;
}
Returns this JPopupMenu component. |
public Component getComponentAtIndex(int i) {
return getComponent(i);
} Deprecated! replaced - by java.awt.Container#getComponent(int)
Returns the component at the specified index. |
public int getComponentIndex(Component c) {
int ncomponents = this.getComponentCount();
Component[] component = this.getComponents();
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = component[i];
if (comp == c)
return i;
}
return -1;
}
Returns the index of the specified component. |
public static boolean getDefaultLightWeightPopupEnabled() {
Boolean b = (Boolean)
SwingUtilities.appContextGet(defaultLWPopupEnabledKey);
if (b == null) {
SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
Boolean.TRUE);
return true;
}
return b.booleanValue();
}
Gets the defaultLightWeightPopupEnabled property,
which by default is true . |
public Component getInvoker() {
return this.invoker;
}
Returns the component which is the 'invoker' of this
popup menu. |
public String getLabel() {
return label;
}
Returns the popup menu's label |
public Insets getMargin() {
if(margin == null) {
return new Insets(0,0,0,0);
} else {
return margin;
}
}
Returns the margin, in pixels, between the popup menu's border and
its containees. |
public MenuKeyListener[] getMenuKeyListeners() {
return listenerList.getListeners(MenuKeyListener.class);
}
Returns an array of all the MenuKeyListener s added
to this JPopupMenu with addMenuKeyListener(). |
public PopupMenuListener[] getPopupMenuListeners() {
return listenerList.getListeners(PopupMenuListener.class);
}
Returns an array of all the PopupMenuListener s added
to this JMenuItem with addPopupMenuListener(). |
JPopupMenu getRootPopupMenu() {
JPopupMenu mp = this;
while((mp!=null) && (mp.isPopupMenu()!=true) &&
(mp.getInvoker() != null) &&
(mp.getInvoker().getParent() != null) &&
(mp.getInvoker().getParent() instanceof JPopupMenu)
) {
mp = (JPopupMenu) mp.getInvoker().getParent();
}
return mp;
}
Returns the popup menu which is at the root of the menu system
for this popup menu. |
public SingleSelectionModel getSelectionModel() {
return selectionModel;
}
Returns the model object that handles single selections. |
public MenuElement[] getSubElements() {
MenuElement result[];
Vector< MenuElement > tmp = new Vector< MenuElement >();
int c = getComponentCount();
int i;
Component m;
for(i=0 ; i < c ; i++) {
m = getComponent(i);
if(m instanceof MenuElement)
tmp.addElement((MenuElement) m);
}
result = new MenuElement[tmp.size()];
for(i=0,c=tmp.size() ; i < c ; i++)
result[i] = tmp.elementAt(i);
return result;
}
Returns an array of MenuElement s containing the submenu
for this menu component. It will only return items conforming to
the JMenuElement interface.
If popup menu is null returns
an empty array. This method is required to conform to the
MenuElement interface. |
public PopupMenuUI getUI() {
return (PopupMenuUI)ui;
}
Returns the look and feel (L&F) object that renders this component. |
public String getUIClassID() {
return uiClassID;
}
Returns the name of the L&F class that renders this component. |
public void insert(Action a,
int index) {
JMenuItem mi = createActionComponent(a);
mi.setAction(a);
insert(mi, index);
}
Inserts a menu item for the specified Action object at
a given position. |
public void insert(Component component,
int index) {
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
int nitems = getComponentCount();
// PENDING(ges): Why not use an array?
Vector< Component > tempItems = new Vector< Component >();
/* Remove the item at index, nitems-index times
storing them in a temporary vector in the
order they appear on the menu.
*/
for (int i = index ; i < nitems; i++) {
tempItems.addElement(getComponent(index));
remove(index);
}
add(component);
/* Add the removed items back to the menu, they are
already in the correct order in the temp vector.
*/
for (Component tempItem : tempItems) {
add(tempItem);
}
}
Inserts the specified component into the menu at a given
position. |
public boolean isBorderPainted() {
return paintBorder;
}
Checks whether the border should be painted. |
public boolean isLightWeightPopupEnabled() {
return lightWeightPopup;
}
Gets the lightWeightPopupEnabled property. |
public boolean isPopupTrigger(MouseEvent e) {
return getUI().isPopupTrigger(e);
}
Returns true if the MouseEvent is considered a popup trigger
by the JPopupMenu 's currently installed UI. |
boolean isSubPopupMenu(JPopupMenu popup) {
int ncomponents = this.getComponentCount();
Component[] component = this.getComponents();
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = component[i];
if (comp instanceof JMenu) {
JMenu menu = (JMenu)comp;
JPopupMenu subPopup = menu.getPopupMenu();
if (subPopup == popup)
return true;
if (subPopup.isSubPopupMenu(popup))
return true;
}
}
return false;
}
Examines the list of menu items to determine whether
popup is a popup menu. |
public boolean isVisible() {
return popup != null;
}
Returns true if the popup menu is visible (currently
being displayed). |
public void menuSelectionChanged(boolean isIncluded) {
if (DEBUG) {
System.out.println("In JPopupMenu.menuSelectionChanged " + isIncluded);
}
if(invoker instanceof JMenu) {
JMenu m = (JMenu) invoker;
if(isIncluded)
m.setPopupMenuVisible(true);
else
m.setPopupMenuVisible(false);
}
if (isPopupMenu() && !isIncluded)
setVisible(false);
}
Messaged when the menubar selection changes to activate or
deactivate this menu. This implements the
javax.swing.MenuElement interface.
Overrides MenuElement.menuSelectionChanged . |
public void pack() {
if(popup != null) {
Dimension pref = getPreferredSize();
if (pref == null || pref.width != getWidth() ||
pref.height != getHeight()) {
popup = getPopup();
} else {
validate();
}
}
}
Lays out the container so that it uses the minimum space
needed to display its contents. |
protected void paintBorder(Graphics g) {
if (isBorderPainted()) {
super.paintBorder(g);
}
}
Paints the popup menu's border if the borderPainted
property is true . |
protected String paramString() {
String labelString = (label != null ?
label : "");
String paintBorderString = (paintBorder ?
"true" : "false");
String marginString = (margin != null ?
margin.toString() : "");
String lightWeightPopupEnabledString = (isLightWeightPopupEnabled() ?
"true" : "false");
return super.paramString() +
",desiredLocationX=" + desiredLocationX +
",desiredLocationY=" + desiredLocationY +
",label=" + labelString +
",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
",margin=" + marginString +
",paintBorder=" + paintBorderString;
}
Returns a string representation of this JPopupMenu .
This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null . |
protected void processFocusEvent(FocusEvent evt) {
super.processFocusEvent(evt);
}
|
protected void processKeyEvent(KeyEvent evt) {
MenuSelectionManager.defaultManager().processKeyEvent(evt);
if (evt.isConsumed()) {
return;
}
super.processKeyEvent(evt);
}
Processes key stroke events such as mnemonics and accelerators. |
public void processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager) {
MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
e.getWhen(), e.getModifiers(),
e.getKeyCode(), e.getKeyChar(),
path, manager);
processMenuKeyEvent(mke);
if (mke.isConsumed()) {
e.consume();
}
}
Processes a key event forwarded from the
MenuSelectionManager and changes the menu selection,
if necessary, by using MenuSelectionManager 's API.
Note: you do not have to forward the event to sub-components.
This is done automatically by the MenuSelectionManager . |
public void processMouseEvent(MouseEvent event,
MenuElement[] path,
MenuSelectionManager manager) {
}
This method is required to conform to the
MenuElement interface, but it not implemented. |
public void remove(int pos) {
if (pos < 0) {
throw new IllegalArgumentException("index less than zero.");
}
if (pos > getComponentCount() -1) {
throw new IllegalArgumentException("index greater than the number of items.");
}
super.remove(pos);
}
Removes the component at the specified index from this popup menu. |
public void removeMenuKeyListener(MenuKeyListener l) {
listenerList.remove(MenuKeyListener.class, l);
}
Removes a MenuKeyListener from the popup menu. |
public void removePopupMenuListener(PopupMenuListener l) {
listenerList.remove(PopupMenuListener.class,l);
}
Removes a PopupMenu listener. |
public void setBorderPainted(boolean b) {
paintBorder = b;
repaint();
}
Sets whether the border should be painted. |
public static void setDefaultLightWeightPopupEnabled(boolean aFlag) {
SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
Boolean.valueOf(aFlag));
}
Sets the default value of the lightWeightPopupEnabled
property. |
public void setInvoker(Component invoker) {
Component oldInvoker = this.invoker;
this.invoker = invoker;
if ((oldInvoker != this.invoker) && (ui != null)) {
ui.uninstallUI(this);
ui.installUI(this);
}
invalidate();
}
Sets the invoker of this popup menu -- the component in which
the popup menu menu is to be displayed. |
public void setLabel(String label) {
String oldValue = this.label;
this.label = label;
firePropertyChange("label", oldValue, label);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, label);
}
invalidate();
repaint();
}
Sets the popup menu's label. Different look and feels may choose
to display or not display this. |
public void setLightWeightPopupEnabled(boolean aFlag) {
// NOTE: this use to set the flag on a shared JPopupMenu, which meant
// this effected ALL JPopupMenus.
lightWeightPopup = aFlag;
}
Sets the value of the lightWeightPopupEnabled property,
which by default is true .
By default, when a look and feel displays a popup,
it can choose to
use a lightweight (all-Java) popup.
Lightweight popup windows are more efficient than heavyweight
(native peer) windows,
but lightweight and heavyweight components do not mix well in a GUI.
If your application mixes lightweight and heavyweight components,
you should disable lightweight popups.
Some look and feels might always use heavyweight popups,
no matter what the value of this property. |
public void setLocation(int x,
int y) {
int oldX = desiredLocationX;
int oldY = desiredLocationY;
desiredLocationX = x;
desiredLocationY = y;
if(popup != null && (x != oldX || y != oldY)) {
popup = getPopup();
}
}
Sets the location of the upper left corner of the
popup menu using x, y coordinates. |
public void setPopupSize(Dimension d) {
Dimension oldSize = getPreferredSize();
setPreferredSize(d);
if (popup != null) {
Dimension newSize = getPreferredSize();
if (!oldSize.equals(newSize)) {
popup = getPopup();
}
}
}
Sets the size of the Popup window using a Dimension object.
This is equivalent to setPreferredSize(d) . |
public void setPopupSize(int width,
int height) {
setPopupSize(new Dimension(width, height));
}
Sets the size of the Popup window to the specified width and
height. This is equivalent to
setPreferredSize(new Dimension(width, height)) . |
public void setSelected(Component sel) {
SingleSelectionModel model = getSelectionModel();
int index = getComponentIndex(sel);
model.setSelectedIndex(index);
}
Sets the currently selected component, This will result
in a change to the selection model. |
public void setSelectionModel(SingleSelectionModel model) {
selectionModel = model;
}
Sets the model object to handle single selections. |
public void setUI(PopupMenuUI ui) {
super.setUI(ui);
}
Sets the L&F object that renders this component. |
public void setVisible(boolean b) {
if (DEBUG) {
System.out.println("JPopupMenu.setVisible " + b);
}
// Is it a no-op?
if (b == isVisible())
return;
// if closing, first close all Submenus
if (b == false) {
// 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
// a protected method and cannot be called from BasicPopupMenuUI directly
// The real solution could be to make
// firePopupMenuCanceled public and call it directly.
Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
if (doCanceled != null && doCanceled == Boolean.TRUE) {
putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
firePopupMenuCanceled();
}
getSelectionModel().clearSelection();
} else {
// This is a popup menu with MenuElement children,
// set selection path before popping up!
if (isPopupMenu()) {
MenuElement me[] = new MenuElement[1];
me[0] = this;
MenuSelectionManager.defaultManager().setSelectedPath(me);
}
}
if(b) {
firePopupMenuWillBecomeVisible();
popup = getPopup();
firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
} else if(popup != null) {
firePopupMenuWillBecomeInvisible();
popup.hide();
popup = null;
firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
// 4694797: When popup menu is made invisible, selected path
// should be cleared
if (isPopupMenu()) {
MenuSelectionManager.defaultManager().clearSelectedPath();
}
}
}
Sets the visibility of the popup menu. |
public void show(Component invoker,
int x,
int y) {
if (DEBUG) {
System.out.println("in JPopupMenu.show " );
}
setInvoker(invoker);
Frame newFrame = getFrame(invoker);
if (newFrame != frame) {
// Use the invoker's frame so that events
// are propagated properly
if (newFrame!=null) {
this.frame = newFrame;
if(popup != null) {
setVisible(false);
}
}
}
Point invokerOrigin;
if (invoker != null) {
invokerOrigin = invoker.getLocationOnScreen();
// To avoid integer overflow
long lx, ly;
lx = ((long) invokerOrigin.x) +
((long) x);
ly = ((long) invokerOrigin.y) +
((long) y);
if(lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE;
if(lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE;
if(ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE;
if(ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE;
setLocation((int) lx, (int) ly);
} else {
setLocation(x, y);
}
setVisible(true);
}
Displays the popup menu at the position x,y in the coordinate
space of the component invoker. |
public void updateUI() {
setUI((PopupMenuUI)UIManager.getUI(this));
}
Resets the UI property to a value from the current look and feel. |