如何从自定义树创建 JPopupMenu
下面的方法从字符串创建自定义数据树。
我正在研究一种生成带有许多子菜单的弹出菜单的方法。基本上这些菜单是动态给出的,所以我有一个算法来处理使用下面的字符串创建子菜单。为了做到这一点,我通过使用分隔符分割来将该字符串转换为 Java 自定义树。
public class MenuItem {
public String Name;
public Vector<MenuItem> Childeren;
public MenuItem() {
Name = "";
Childeren = new Vector<MenuItem>();
}
public MenuItem(String name) {
this();
Name = name;
}
public String toString() {
return Name + " " + Childeren ;
}
}
public static int createNode(StringTokenizer p_jTokenizer, MenuItem p_iParent) {
int nCount = 0;
while(p_jTokenizer.hasMoreTokens()) {
String strToken = p_jTokenizer.nextToken();
MenuItem iItem = new MenuItem();
if(strToken.endsWith("[")) {
strToken = strToken.substring(0, strToken.length() - 1);
nCount = createNode(p_jTokenizer, iItem);
}
while(strToken.endsWith("]")) {
nCount++;
strToken = strToken.substring(0, strToken.length() - 1);
}
iItem.Name = strToken;
p_iParent.Childeren.add(iItem);
while(nCount > 0) {
return --nCount;
}
}
return nCount;
}
我解析的动态字符串值的示例:
String str = "Menu1;Menu2[;Menu2A;Menu2B[;Menu2B-A;Menu2B-B]];Menu3;";
当前的方法创建一个如下结构的树:
Pop-up Menu
/ | \
Menu1 Menu2 Menu3
/ |
Menu2A Menu2B
| \
| \
Menu2B-A Menu2B-B
我被困在如何基于该树创建带有子菜单的 JPopUpMenu 上。我想到使用递归方式,但不确定该走哪条路。
The method below creates Custom Data Tree from A String.
I am working on a method that generates pop-up menu with many submenus. Basically these menus given dynamically so I have an algorithm to handle creating submenus using below string. In order to do it I transform this string into Java Custom Tree by splitting using the delimiter.
public class MenuItem {
public String Name;
public Vector<MenuItem> Childeren;
public MenuItem() {
Name = "";
Childeren = new Vector<MenuItem>();
}
public MenuItem(String name) {
this();
Name = name;
}
public String toString() {
return Name + " " + Childeren ;
}
}
public static int createNode(StringTokenizer p_jTokenizer, MenuItem p_iParent) {
int nCount = 0;
while(p_jTokenizer.hasMoreTokens()) {
String strToken = p_jTokenizer.nextToken();
MenuItem iItem = new MenuItem();
if(strToken.endsWith("[")) {
strToken = strToken.substring(0, strToken.length() - 1);
nCount = createNode(p_jTokenizer, iItem);
}
while(strToken.endsWith("]")) {
nCount++;
strToken = strToken.substring(0, strToken.length() - 1);
}
iItem.Name = strToken;
p_iParent.Childeren.add(iItem);
while(nCount > 0) {
return --nCount;
}
}
return nCount;
}
An Example of Dynamic String Values that I parse:
String str = "Menu1;Menu2[;Menu2A;Menu2B[;Menu2B-A;Menu2B-B]];Menu3;";
The current method creates a tree like below structure:
Pop-up Menu
/ | \
Menu1 Menu2 Menu3
/ |
Menu2A Menu2B
| \
| \
Menu2B-A Menu2B-B
I am stuck on how to create JPopUpMenu with submenus based on this tree. I think of using recursive way but not sure which way to go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遍历树结构。如果一个元素是节点,则创建 JMenu;如果它是叶元素,则创建 JMenuItem,将所有子元素添加到 JMenu。
Iterate through the tree structure. If an element is node create JMenu if it's leaf create JMenuItem add all the children to the JMenu.