How I can implement dynamic Jtree, which shows created instances of clases?
For example, I can create new Book(name) in my app. In every book can be chapters = ArrayList of Chapter. And now How I can do a jtree from it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Class involved in JTree usage are the following:
JTreeclass itself which provides the displayable item you need and it works exactly like tables and lists in swing: they have a model!DefaultTableModel implements TableModelwhich works as a data container for aJTree. It is istantiated with a root node, then every children is added to root node or to other nodes contained in the tree.DefaultMutableTreeNodewhich is a general purpose default implementation for a generic JTree node.How to mix up these things? First of all I suggest you to check java sun guide about it here but to have a quick look you can think about having something like this:
Approach is really identical to the one you use for
JTableorJListalso if data structure (and so model) differs. Think about that this is the default way to do it but you can easily write your ownTreeNodeorTreeModelclass according to what you really need.I would like to let you know that sun’s guide about java contains almost every topic contained in the basic JDK so it’s a good thing to give it a look before feeling lost.