1

I am having issues accessing the elements of my ArrayList just clicking in the corresponding node. 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 MainClass: ArrayList of animal. Main is running here.
  • class Menu: JFrame designed with NetBeans. To get the elements of the ArrayList I use the following code:

    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.

In my Menu class I have also different JTextPanel for the different attributes of the animals.

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.

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?

3
  • This is one way, another way might use the TreeSelectionModel to detect changes in the dejection which might be a cleaner solution as it includes the possibility of the user the key board. The DefaultMutatableTreeNode provides the associated object via the userObject property Commented Jun 9, 2016 at 10:51
  • Of and I wouldn't reconstruct the tree every time you want to add/remove elements, I'd make use of the TreeModel to generate update events Commented Jun 9, 2016 at 10:58
  • And for a larger example Commented Jun 9, 2016 at 11:12

1 Answer 1

2

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();
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help. I will check through that code and hopefully will be able to solve my issue. Best regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.