我第一次使用java Swing,我想知道是否可以为每个DefaultMutableTreeNode添加值并通过选择特定节点来引用它们?
e.g。通过从Jtree模型中选择节点来调用特定的PowerShell脚本(1个节点= 1个脚本)。 我真的是新手,如果有人可以解释或重定向我的某些解决方案,我会很高兴; p
private JTree setTab2Tree() {
JTree tree;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Customers");
DefaultMutableTreeNode webClient = new DefaultMutableTreeNode("Web");
webClient.add(new DefaultMutableTreeNode("Person Customer"));
webClient.add(new DefaultMutableTreeNode("Firm Customer"));
DefaultMutableTreeNode appClient = new DefaultMutableTreeNode("App");
appClient.add(new DefaultMutableTreeNode("Person Customer"));
appClient.add(new DefaultMutableTreeNode("Firm Customer"));
root.insert(webClient, 0);
root.insert(appClient, 1);
tree = new JTree(root);
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new MainTree.SelectionListener());
tree.setBorder(BorderFactory.createLineBorder(Color.black));
add(new JScrollPane(tree));
}