Is it possible to get the position/index of the JMenuItem inside its JMenu?
是否有可能在其JMenu中获取JMenuItem的位置/索引?
For instance, I have a JMenu called "Search" and 3 JMenuItems called "Find", "Count" and "Replace", and I have just 1 class manager to manage all the 3 JMenuItems. Hence I use the getSource() method to obtain which of the JMenuItems are clicked, so that I can pass the index of the JMenuItem to the constructor of another class.
例如,我有一个名为“Search”的JMenu和3个名为“Find”,“Count”和“Replace”的JMenuItems,我只有1个类管理器来管理所有3个JMenuItems。因此,我使用getSource()方法来获取单击了哪个JMenuItem,以便我可以将JMenuItem的索引传递给另一个类的构造函数。
My problem is if wanted to add more JMenuItems or to change the order of the JMenuItems, I would have to change the indexes passed to the constructor of the other class.
我的问题是如果想要添加更多JMenuItems或更改JMenuItems的顺序,我将不得不更改传递给另一个类的构造函数的索引。
Thank you!
谢谢!
2 个解决方案
#1
1
Use JMenuBar#getMenu() and JMenu#getMenuComponent() methods to get all the JMenu and JMenuItem.
使用JMenuBar#getMenu()和JMenu#getMenuComponent()方法获取所有JMenu和JMenuItem。
Just iterate all the JMenuItem
of any JMenu
and get the index of desired JMenuItem
.
只需迭代任何JMenu的所有JMenuItem并获取所需JMenuItem的索引。
sample program:
示例程序:
JMenuBar menubar1 = getJMenuBar();
for (int i = 0; i < menubar1.getMenuCount(); i++) {
JMenu menu1 = menubar1.getMenu(i);
System.out.println("Menu:" + menu1.getText());
for (int j = 0; j < menu1.getMenuComponentCount(); j++) {
java.awt.Component comp = menu1.getMenuComponent(j);
if (comp instanceof JMenuItem) {
JMenuItem menuItem1 = (JMenuItem) comp;
System.out.println("MenuItem:" + menuItem1.getText());
}
}
}
#2
0
Get position/index of JMenuItem from JMenu
Creating a sample nested context menu
创建示例嵌套上下文菜单
JPopupMenu popup = new JPopupMenu();
popup.setName("popup");
JMenu jMenu= new JMenu("menu");
jMenu.setName("menu");
JMenuItem menuItem1 = new JMenuItem("sub1");
jMenu.add(menuItem1);
menuItem1.addActionListener(this);
popup.add(jMenu);
Getting the position/index of JMenuItem from JMenu
从JMenu获取JMenuItem的位置/索引
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
try{
JMenuItem menuItem = (JMenuItem) e.getSource();
JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
Component invoker = popupMenu.getInvoker();
// Print MenuItem index against the total number of items
System.out.println(popupMenu.getComponentZOrder(menuItem)
+"/"+popupMenu.getComponentCount());
}catch(Exception ex){
ex.printStackTrace();
}
}
#1
1
Use JMenuBar#getMenu() and JMenu#getMenuComponent() methods to get all the JMenu and JMenuItem.
使用JMenuBar#getMenu()和JMenu#getMenuComponent()方法获取所有JMenu和JMenuItem。
Just iterate all the JMenuItem
of any JMenu
and get the index of desired JMenuItem
.
只需迭代任何JMenu的所有JMenuItem并获取所需JMenuItem的索引。
sample program:
示例程序:
JMenuBar menubar1 = getJMenuBar();
for (int i = 0; i < menubar1.getMenuCount(); i++) {
JMenu menu1 = menubar1.getMenu(i);
System.out.println("Menu:" + menu1.getText());
for (int j = 0; j < menu1.getMenuComponentCount(); j++) {
java.awt.Component comp = menu1.getMenuComponent(j);
if (comp instanceof JMenuItem) {
JMenuItem menuItem1 = (JMenuItem) comp;
System.out.println("MenuItem:" + menuItem1.getText());
}
}
}
#2
0
Get position/index of JMenuItem from JMenu
Creating a sample nested context menu
创建示例嵌套上下文菜单
JPopupMenu popup = new JPopupMenu();
popup.setName("popup");
JMenu jMenu= new JMenu("menu");
jMenu.setName("menu");
JMenuItem menuItem1 = new JMenuItem("sub1");
jMenu.add(menuItem1);
menuItem1.addActionListener(this);
popup.add(jMenu);
Getting the position/index of JMenuItem from JMenu
从JMenu获取JMenuItem的位置/索引
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
try{
JMenuItem menuItem = (JMenuItem) e.getSource();
JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
Component invoker = popupMenu.getInvoker();
// Print MenuItem index against the total number of items
System.out.println(popupMenu.getComponentZOrder(menuItem)
+"/"+popupMenu.getComponentCount());
}catch(Exception ex){
ex.printStackTrace();
}
}