简体   繁体   English

JAVA-如何从JTree访问ArrayList的对象

[英]JAVA - How to access objects of ArrayList from JTree

I am having issues accessing the elements of my ArrayList just clicking in the corresponding node. 我在单击相应节点时访问ArrayList的元素时遇到问题。 This is the first time I am using this, I have searched for a lot of different tutorials but I could not solve my issue. 这是我第一次使用此工具,我搜索了许多不同的教程,但无法解决我的问题。 This is what I have so far: 这是我到目前为止的内容:

  • class Animal : Just constructor with diffrent arguments, setters and getters. class Animal :只是具有不同参数,setter和getter的构造函数。
  • class MainClass : ArrayList of animal. class MainClass :动物的ArrayList。 Main is running here. Main在这里运行。
  • class Menu : JFrame designed with NetBeans. class Menu :使用NetBeans设计的JFrame。 To get the elements of the ArrayList I use the following code: 要获取ArrayList的元素,请使用以下代码:

     public void refreshTree(){ root = new DefaultMutableTreeNode("Animals"); children1 = new DefaultMutableTreeNode("Carnivores"); root.add(children1); mainTree = new JTree(root); List<Animal> animals = mainClass.returnList(); for(Animal animal: animals){ DefaultMutableTreeNode node = new DefaultMutableTreeNode(animal); children1.add(node); } jScrollPane2.setViewportView(mainTree); } 

Everytime that I add or remove an element from the ArrayList, I use refreshTree() method in order to get an updated version of my ArrayList. 每次我从ArrayList中添加或删除元素时,我都会使用refreshTree()方法来获取ArrayList的更新版本。

In my Menu class I have also different JTextPanel for the different attributes of the animals. 在我的Menu类中,对于动物的不同属性,我还具有不同的JTextPanel。

What I need to do (I will not copy the code I have right now, because I think has not sense) is to be able to click in one node, so the different attributes of the animal are loaded in the JTextPanel, so if I modify the values, they will be changed in the object as well. 我需要做的事情(我不会复制我现在拥有的代码,因为我认为没有意义)是能够单击一个节点,因此将动物的不同属性加载到JTextPanel中,所以如果我修改值,它们也会在对象中更改。

I know I should use something like: 我知道我应该使用类似:

int value = textPanel.getText();

And then use the setters of my class Animal. 然后使用我的动物课程的二传手。

My problem is how I access to that specific animal just clicking in the JTree? 我的问题是,仅单击JTree,如何访问该特定动物?

You can do like below. 您可以像下面这样。 Hope it helps you to progress. 希望它能帮助您进步。

JTree tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Animals");
DefaultTreeModel model = new DefaultTreeModel(root);

tree.setModel(model);
root.add(new DefaultMutableTreeNode(new Animal("Dog","ACS")));
root.add(new DefaultMutableTreeNode(new Animal("Cat","BCS")));
root.add(new DefaultMutableTreeNode(new Animal("Lion","FCS")));

DefaultTreeSelectionModel sModel = new DefaultTreeSelectionModel(); 

sModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setSelectionModel(sModel);
tree.addTreeSelectionListener(new TreeSelectionListener() {

    @Override
    public void valueChanged(TreeSelectionEvent selection) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)selection.getPath().getLastPathComponent();
        if(selectedNode.isLeaf()) {
            Animal animal = (Animal)selectedNode.getUserObject();
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM