Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access objects of ArrayList from JTree  RSS feed

 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

The other day I was asking a doubt about a database and today I am coming with another one. I am really sorry to ask so much, but lately I am writting a lot of code and "playing" with new features that I have not used before.

I am having issues accessing the elements of my ArrayList just clicking in the corresponding node. This is the first time I am using the JTree, I have searched for a lot of different tutorials but I could not solve my issue. I will explain you what I have:

class Animal: Just constructor with different 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 next code:



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:



And then use the setters of my class Animal.

My problem is how I access to that specific animal just clicking in the JTree? It should be something like:



Thank you so much in advance.

Best regards.
 
Salil Wadnerkar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not understand fully. But, I think you should store the state of your GUI - which element of the arraylist is clicked - somewhere in the application and set it from the click handler. So, suppose you are displaying 10 nodes of the tree to the user, and user clicks on 2nd, you should override the click handler and store this index somewhere so that in your refreshHandler, you know which element to get data from.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose Lara wrote:My problem is how I access to that specific animal just clicking in the JTree? It should be something like:



I suppose you haven't read the JTree tutorial so you should put aside your code and spend some time doing that. So much of what you are doing there is just the wrong way to do things in Swing.

You should never use MouseListeners unless you really have to. Swing almost always has more convenient tools than that. If you want to get the data from the tree node when you click on it, then you just use a TreeSelectionListener. That's near the beginning of the tutorial. And once you have got the selected node, it should already contain an Animal object. Make sure your Animal class has a suitable toString() method because that is what Swing uses to display an Animal on the screen.

And your idea of refreshing the tree every time you change something in that list is hard to understand too. For one thing you didn't say how you were changing the list -- wouldn't it be easier to update the tree directly? Seems like you're letting your code force the design rather than having the design tell you what code to write.
 
Jose Lara
Greenhorn
Posts: 23
2
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Jose Lara wrote:My problem is how I access to that specific animal just clicking in the JTree? It should be something like:



I suppose you haven't read the JTree tutorial so you should put aside your code and spend some time doing that. So much of what you are doing there is just the wrong way to do things in Swing.

You should never use MouseListeners unless you really have to. Swing almost always has more convenient tools than that. If you want to get the data from the tree node when you click on it, then you just use a TreeSelectionListener. That's near the beginning of the tutorial. And once you have got the selected node, it should already contain an Animal object. Make sure your Animal class has a suitable toString() method because that is what Swing uses to display an Animal on the screen.

And your idea of refreshing the tree every time you change something in that list is hard to understand too. For one thing you didn't say how you were changing the list -- wouldn't it be easier to update the tree directly? Seems like you're letting your code force the design rather than having the design tell you what code to write.


Hello Paul. I will check about the TreeSelectionListener and hopefully it will work.

About just refreshing the tree everytime I change something, I tried but for some reason it was not updating properly. When I deleted some object, it was not deleted from the node after refreshing. For sure I was doing something wrong, but after fighting a lot with that, finally I decided to take this not so good solution. As I said before, this is the first time I am working with these things.

Thank you so much for your answer.

Best regards.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose Lara wrote:About just refreshing the tree everytime I change something, I tried but for some reason it was not updating properly. When I deleted some object, it was not deleted from the node after refreshing. For sure I was doing something wrong, but after fighting a lot with that, finally I decided to take this not so good solution.


That would most likely be because you aren't notifying the tree's model when you change things. That's covered in the tutorial as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!