【问题标题】:JPopupMenu know which JMenuItem are clickedJPopupMenu 知道点击了哪个 JMenuItem
【发布时间】:2014-03-26 09:40:56
【问题描述】:

我的问题很简单。我有一个JPopupMenu,它显示了两个JMenuItem。我发现使用

知道单击了哪个项目的唯一方法
class MenuActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    System.out.println("Selected: " + e.getActionCommand());    
  }
}

但是命令e.getActionCommand() 打印项目内的文本。我想获得一个索引from 0 to n 来知道单击了哪个项目而不是文本(可以修改)。 有可能吗?

【问题讨论】:

  • 因为我需要得到一个int 而不是一个字符串......在我的情况下使用int 会更容易。但如果不可能,我已经找到了解决方法。
  • 很抱歉试图提供帮助
  • 问题,我能想到几个,但没有一个能帮上忙,可能只是浪费彼此的时间......

标签: java swing jmenuitem jpopupmenu


【解决方案1】:

你可以...

将每个JMenuItem 放在Map 中,并使用您想要的int

Map<JMenuItem, Integer> menuMap = new HashMap<JMenuItem, Integer>(25);
//...
JMenuItem item1 = ...
menuMap.put(item, 0);
JMenuItem item2 = ...
menuMap.put(item, 1);

然后在ActionListener中,您只需根据事件来源查找它...

public void actionPerformed(ActionEvent e) {
    JMenuItem item = (JMenuItem)e.getSource();
    int index = menuMap.get(item);

你可以...

使用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);

你可以...

利用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"));

你可以...

设置项目的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");

问题在于您必须将ActionEventactionCommand 解析回int...不是真正的隔音解决方案...

你可以...

设置每个JMenuItemclientProperty

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);

但这会变得一团糟……

public void actionPerformed(ActionEvent e) {
    JMenuItem item = (JMenuItem)e.getSource();
    Object value = item.getClientProperty("keyValue");
    if (value instanceof Integer) {
        int index = ((Integer)value).intValue();

可能还有其他解决方案,但不知道您为什么要这样做,因此无法提出准确的建议......对不起

【讨论】:

    【解决方案2】:

    下面的代码展示了如何获取选中的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);
                }
          }
    

    【讨论】:

    • 这种方法的问题是它没有考虑到JSeparators 或其他可能在弹出窗口中的组件
    • @MadProgrammer 你是对的,添加一个instanceof JMenuItem 应该可以解决这个问题,不是吗?
    • 问题,不知道为什么 OP 想要的是 int 值,获取组件索引可能不会导致他们所追求的......
    • @MadProgrammer 看来他不想分享。
    【解决方案3】:

    为什么我们不能这样做:

    menu.getSelectionModel().getSelectedIndex()
    

    返回int?

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 1970-01-01
      • 2017-11-17
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多