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.
TrayIcon
JPopupMenu
JMenuItem
getBounds()
getSize()
getVisibleRect()
As you can see in the image, the pop-up menu certainly has a dimension.Now, how can I get the size of the popup menu?
Components do not have size until they are implemented. This basically means until they become visible (or packaged).
, ? , , .
, PopupMenuListener popupMenuWillBecomeVisible(). SwingUtilities.invokeLater(), .
, JPopupMenu TrayIcon AWT PopupMenu. JPopupMenu pack(), , . , , :myjPop.setVisible(true);myjPop.setVisible(false);. -, .
pack()
myjPop.setVisible(true);
myjPop.setVisible(false);
. show() .
, , , . :
. popmenu
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);