[英]JPopupMenu know which JMenuItem are clicked
my question in very simple. 我的问题很简单。 I have a
JPopupMenu
that shown two JMenuItem
. 我有一个
JPopupMenu
,显示了两个JMenuItem
。 The only way I have found to know which item are clicked using 我发现知道点击使用哪个项目的唯一方法
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + e.getActionCommand());
}
}
but the command e.getActionCommand()
print the text inside the item. 但是命令
e.getActionCommand()
在项目内部打印文本。 I would like get an index from 0 to n
to known which item are clicked and not the text (that can be modified). 我想获得一个
from 0 to n
的索引from 0 to n
以了解单击了哪个项目,而不是单击文本(可以修改)。 Is it possible ? 可能吗 ?
Place each JMenuItem
in a Map
, with the int
value you want 将每个
JMenuItem
放置在Map
,并带有所需的int
值
Map<JMenuItem, Integer> menuMap = new HashMap<JMenuItem, Integer>(25);
//...
JMenuItem item1 = ...
menuMap.put(item, 0);
JMenuItem item2 = ...
menuMap.put(item, 1);
Then in the ActionListener
, you would simply look it up based on the source of the event... 然后在
ActionListener
,您只需根据事件的来源对其进行查找...
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
int index = menuMap.get(item);
Use a List
and determine the index of the JMenuItem
within the list... 使用
List
并确定列表中JMenuItem
的索引...
List<JMenuItem> menuList = new ArrayList<JMenuItem>(25);
//...
JMenuItem item1 = ...
menuList.add(item);
JMenuItem item2 = ...
menuList.add(item);
//...
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
int index = menuList.indexOf(item);
Make use of the Action
API 利用
Action
API
public class IndexedAction extends AbstractAction {
private int index;
public IndexedAction(int index, String name) {
this.index = index;
putValue(NAME, name);
}
@Override
public void actionPerformed(ActionEvent e) {
// Use the index some how...
}
}
//...
JPopupMenu menu = new JPopupMenu();
menu.add(new IndexedAction(0, "Item 1"));
menu.add(new IndexedAction(1, "Item 2"));
menu.addSeparator();
menu.add(new IndexedAction(2, "Item 3"));
menu.add(new IndexedAction(3, "Item 4"));
Set the actionCommand
property of the items... 设置项目的
actionCommand
属性...
JPopupMenu pm = ...;
pm.add("Item 1").setActionCommand("0");
pm.add("Item 2").setActionCommand("1");
menu.addSeparator();
pm.add("Item 3").setActionCommand("2");
pm.add("Item 4").setActionCommand("3");
The problem with this is you're going to have to parse the actionCommand
of the ActionEvent
back to an int
...not a really sound proof solution... 这样做的问题是,您将不得不将
ActionEvent
的actionCommand
解析回一个int
……不是真正可靠的解决方案...
Set the clientProperty
of each JMenuItem
设置
clientProperty
每个JMenuItem
JPopupMenu pm = ...;
pm.add("Item 1").putClientProperty("keyValue", 0);
pm.add("Item 2").putClientProperty("keyValue", 1);
menu.addSeparator();
pm.add("Item 3").putClientProperty("keyValue", 2);
pm.add("Item 4").putClientProperty("keyValue", 3);
But this gets messy... 但这很混乱...
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
Object value = item.getClientProperty("keyValue");
if (value instanceof Integer) {
int index = ((Integer)value).intValue();
There's probably other solutions, but without knowing why you want to do this, it makes it impossible to make an accurate suggestion...sorry 可能还有其他解决方案,但是由于不知道为什么要这样做,因此无法提出准确的建议...对不起
The code below shows how to get the index of selected JMenuItem
: 下面的代码显示如何获取所选
JMenuItem
的索引:
class MenuActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem menuitem=(JMenuItem) e.getSource();
JPopupMenu popupMenu =(JPopupMenu) menuitem.getParent();
int index= popupMenu.getComponentIndex(menuitem);
System.out.println("index:"+index);
}
}
Why can't we do: 我们为什么不能做:
menu.getSelectionModel().getSelectedIndex()
which returns an int
? 哪个返回一个
int
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.