I have been trying a lot of approaches on how to limit my code on adding nodes only to the 1st level. This means that the user can only add nodes to the 1st level children of a JTree.
我一直在尝试很多方法来限制我的代码只在第一级添加节点。这意味着用户只能将节点添加到JTree的第一级子节点。
Adding a node in my program can be done in 2 ways 1. Add node button 2. Select > Right click > Add node (In here, I wanted to disable this behavior if it a non 1st level child is selected. It's a long shot though )
在我的程序中添加节点可以通过两种方式完成1.添加节点按钮2.选择>右键单击>添加节点(在此处,如果选择非第一级子节点,我想禁用此行为。这是一个远景虽然)
I need a validation that permits the addition of nodes in other levels. Thanks!
我需要一个允许在其他级别添加节点的验证。谢谢!
Here is the working code:
这是工作代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class ProblemTree extends JFrame {
private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
private DefaultTreeModel model = new DefaultTreeModel(root);
private JTree tree = new JTree(model);
private JButton addButton = new JButton("Add Node to 1st level only");
public ProblemTree() {
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(
"1st level: Child 1");
n1.add(new DefaultMutableTreeNode("2nd level: Child l"));
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(
"1st level: Child 2");
n2.add(new DefaultMutableTreeNode("2nd level: Child 2"));
DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(
"1st level: Child 3");
n3.add(new DefaultMutableTreeNode("2nd level: Child 3"));
root.add(n1);
root.add(n2);
root.add(n3);
tree.setEditable(true);
tree.setSelectionRow(0);
tree.setRootVisible(true);
tree.setShowsRootHandles(true);
final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem runTask = new JMenuItem("New Node for 1st level only =( ");
runTask.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (selNode == null) {
return;
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
"New Node");
model.insertNodeInto(newNode, selNode, selNode.getChildCount());
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
tree.setSelectionPath(path);
tree.startEditingAtPath(path);
}
});
popupMenu.add(runTask);
tree.setComponentPopupMenu(popupMenu);
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (selNode == null) {
return;
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
"New Node");
model.insertNodeInto(newNode, selNode, selNode.getChildCount());
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
tree.setSelectionPath(path);
tree.startEditingAtPath(path);
}
});
panel.add(addButton);
getContentPane().add(panel, BorderLayout.SOUTH);
setSize(700, 400);
setVisible(true);
}
public static void main(String[] arg) {
ProblemTree pt = new ProblemTree();
}
}
1 个解决方案
#1
just replace
tree.setComponentPopupMenu(popupMenu);
with:
tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
int row = tree.getClosestRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(row);
if (tree.getSelectionPath().getPathCount() == 2) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
});
and to control your button add:
并控制你的按钮添加:
addButton.setEnabled(false);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
addButton.setEnabled((tree.getSelectionPath().getPathCount() == 2));
}
});
#1
just replace
tree.setComponentPopupMenu(popupMenu);
with:
tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
int row = tree.getClosestRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(row);
if (tree.getSelectionPath().getPathCount() == 2) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
});
and to control your button add:
并控制你的按钮添加:
addButton.setEnabled(false);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
addButton.setEnabled((tree.getSelectionPath().getPathCount() == 2));
}
});