从 JMenu 中删除所有 JMenuItem
我把 JMenu 放下了,除了删除。 :DI 的意思是,我可以执行 popup.remove(NUMBER)
但这可能会导致 NPE 错误。那么,有没有办法从 JMenu
中删除所有 JMenuItems
呢?
如果有人感兴趣的话,这是我的更新checkPopup()
:
private void checkPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
int itemSelectx = listbox.getSelectedIndex();
Object actItemx = listbox.getModel().getElementAt(itemSelectx);
System.out.println("You pressed on " + actItemx);
if (actItemx == "Item 1") {
popup.add(cancelMenuItem); // add the ability to cancel an item
popup.add(dropMenuItem); // add ability to drop the item
}
popup.show(inv.this, e.getX(), e.getY()); // show item at mouse
popup.revalidate(); // revalidate
//popup.remove(0); // removing first (0) menu item
}
}
快完成了! :) (是的,我尝试过 Google 和 JavaDocs)
I got the JMenu down except for removing. :D I mean, I can do popup.remove(NUMBER)
but that can cause NPE errors. So, is there a way to remove all JMenuItems
from JMenu
?
Here's my update checkPopup()
if anyone's interested:
private void checkPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
int itemSelectx = listbox.getSelectedIndex();
Object actItemx = listbox.getModel().getElementAt(itemSelectx);
System.out.println("You pressed on " + actItemx);
if (actItemx == "Item 1") {
popup.add(cancelMenuItem); // add the ability to cancel an item
popup.add(dropMenuItem); // add ability to drop the item
}
popup.show(inv.this, e.getX(), e.getY()); // show item at mouse
popup.revalidate(); // revalidate
//popup.remove(0); // removing first (0) menu item
}
}
Almost there! :) (yes, I tried Google and JavaDocs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解了您的要求,那么您需要
JMenu
上的removeAll()
方法;请参阅此处。If I've understood what you're after correctly, you want the
removeAll()
method onJMenu
; See the Javadoc here.