How to get JPopupMenu size?

I have TrayIconwith attached to it JPopupMenu. When I add JMenuItemto the popup menu, I want to know the size of this popup menu. But size is not calculated: getBounds(), getSize()and getVisibleRect()all return to the measurement of zero-zero.

As you can see in the image, the pop-up menu certainly has a dimension.
Tray Popup Menu
Now, how can I get the size of the popup menu?

+5
source share
3 answers

Components do not have size until they are implemented. This basically means until they become visible (or packaged).

, ? , , .

, PopupMenuListener popupMenuWillBecomeVisible(). SwingUtilities.invokeLater(), .

+5

, JPopupMenu TrayIcon AWT PopupMenu. JPopupMenu pack(), , . , , :
myjPop.setVisible(true);
myjPop.setVisible(false);
. -, .

0

. show() .

, , , . :

. popmenu enter image description here

menu.getWidth() , 0 , , ;

int firstX = refreshButton.getLocation().x;
int firstY = refreshButton.getLocation().y;
menu.show(toolBar, firstX, firstY);
menu.show(toolBar, firstX -  menu.getWidth(), firstY);
0

All Articles