我正在尝试动态地将节点添加到弹出组件内的JTree和JMenuItem组件的click动作事件中。我右键单击节点组件并添加另一个节点。节点的添加适用于叶节点,但不适用于非叶节点。我不知道代码有什么问题。似乎添加了节点,但Gui没有更新。
平台: Windows 7
爪哇 java版“1.7.0_17” Java(TM)SE运行时环境(版本1.7.0_17-b02) Java HotSpot(TM)64位服务器VM(内置23.7-b01,混合模式)
package test;
/**
*
* @author Rajan
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
/**
*
* @author Rajan Prasad Upadhyay
*/
public class TreeTest {
String name = "name";
public static void main(String[] args) {
javax.swing.JFrame topFrame = new JFrame();
javax.swing.JPanel topPanel = new JPanel();
javax.swing.JScrollPane leftScrollPane = new JScrollPane();
javax.swing.JTree leftTree = new JTree();
topFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//layouts
topFrame.setLayout(new BorderLayout());
topPanel.setLayout(new BorderLayout());
// addition of components
// topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
topPanel.add(leftScrollPane);
leftScrollPane.getViewport().add(leftTree);
addExperimentEvents(leftTree);
topFrame.setSize(600, 500);
topFrame.setContentPane(topPanel);
topFrame.setVisible(true);
}
public static void addExperimentEvents(JTree tree) {
MouseAdapter ma = new MouseAdapter() {
private void myPopupEvent(MouseEvent e) {
int x = e.getX();
int y = e.getY();
final JTree tree = (JTree) e.getSource();
TreePath path = tree.getPathForLocation(x, y);
if (path == null) {
return;
}
tree.setSelectionPath(path);
Object obj = (Object) path.getLastPathComponent();
String label = "Add:";// + obj.toString();
JPopupMenu popup = new JPopupMenu("Menu");
JMenu sectionsMenu = new JMenu("Sections");
JMenuItem menuItem1 = new JMenuItem("Item1");
sectionsMenu.add(menuItem1);
JMenuItem menuItem2 = new JMenuItem("Item2");
sectionsMenu.add(menuItem2);
JMenuItem item = new JMenuItem(label);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Editing the tree");
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
TreeNode root = (TreeNode) tree.getModel().getRoot();
DefaultTreeModel model = new DefaultTreeModel(root);
if (selNode != null) {
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + (selNode.getChildCount() + 1));
model.insertNodeInto(newNode, selNode, selNode.getChildCount());//
//model.insertNodeInto(newNode, (MutableTreeNode) root, root.getChildCount());
tree.expandPath(new TreePath(model.getPathToRoot(newNode.getParent())));
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
System.out.println(selNode.getChildCount() + ", " + path);
tree.scrollPathToVisible(path);
tree.setSelectionPath(path);
tree.startEditingAtPath(path);
model.reload((TreeNode) model.getRoot());
}else{
System.out.println("selNode is null");
}
}
});
//addHoverAction(item);//
//item.add(new JMenuItem("page"));
popup.add(item);
popup.add(new JMenuItem("Move: "));
popup.add(sectionsMenu);
popup.show(tree, x, y);
}
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
myPopupEvent(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
myPopupEvent(e);
}
}
};
tree.addMouseListener(ma);
}
public static void addHoverAction(final JComponent com) {
MouseAdapter adapter = new MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
System.out.println("mouse Entered: ");
if (true) {
JPopupMenu pop = new JPopupMenu();
pop.add(new JMenuItem("Node"));
pop.show(com.getParent(), com.getWidth(), com.getHeight());
}
}
};
com.addMouseListener(adapter);
}
}
首先感谢您提供解决方案和问题描述。
答案 0 :(得分:2)
使用以下行
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
而不是
DefaultTreeModel model = new DefaultTreeModel(root);
最后展开所需的节点。
的示例代码答案 1 :(得分:1)
有一个Swing tutorial that does exactly that。您可以看到它们用于动态添加,删除和编辑树的完整代码。他们的代码很短。