Skip to main content

JXTreeTable, AbstractTreeTableModel - How to Update changes / get TreePath ?

4 replies [Last post]
AspirinC
Offline
Joined: 2012-06-19
Points: 0

Hello everybody,

I currently working with a JXTreeTable with an own AbstractTreeTableModel.
When I edit a Cell in the TreeTable I also want the Model to be updated and display the change.
My Problem is that in my setValueAt() Method (in my Custom AbstractTreeTableModel) I want to inform the listeners with a fireXXX event - but therefore I need a TreePath. Is there an easy way to get it? Or have I construct it on my own? Is there a best practice?

Here is some of my code:

public class CommandTableModel extends AbstractTreeTableModel {
...

@Override
public void setValueAt(Object value, Object node, int col) {
...
this.modelSupport.fireChildrenChanged(????);
}
...
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

your model needs to implement some method which returns the path to the root: that's highly dependent on the exact nature of your nodes, so nothing any general implementation can provide. For pointers, see DefaultTreeTableModel.

Cheers
Jeanette

AspirinC
Offline
Joined: 2012-06-19
Points: 0

Thank you for the answer. :)

I also have a more general question about the usage of TreeTables.
First of all my situation: I want to use the JXTreeTables to display and to edit (delete, change, add ) my data model. My data model consists of many classes which are related.
To get more specific: I have an abstract class "Element" which is the super class for other classes like "SpecialElement" and "Folder" (Folder has a List of Elements).
The elements are stored in a List ... this is how I archive the hierarchical order.

For this case I assume the following structure would be a good practice:
The abstract class "Element" should implement "MutableTreeTableNode" in order to provide the functions you mentioned (getParent() etc) for every subclass.
My CustomTreeTableModel extends AbstractTreeTableModel and in the setValueAt() Method I update my model. To inform the Listeners I will call fireChildChanged with the treePath I'll get from the Element (which implements MutableTreeTableNode).
Am I right so far? :D

I haven't looked yet into how to add & remove Elements. First thought would be that setValueAt() also handles the removal but how to add?

Thanks in advance for reading :)

kleopatra
Offline
Joined: 2003-06-11
Points: 0

actually, I didn't read it in any depth, just skimmed ;-)

Sounds fishy to use setValueAt for insertion/removal, typically your custom model will have dedicated methods for doing such. Have a look a DefaultTree/Table/Model for pointers Keep in mind: a TreeTableModel _is-a_ TreeModel, so the best path for learning is to understand the core TreeModel.

Cheers
Jeanette

AspirinC
Offline
Joined: 2012-06-19
Points: 0

Thanks for your help ...

Now I've got another short Question:
I've got one DataStructure which represents my hierarchy but I want to use two views.
First view is the JXtreeTable and the second one is a Jtree.
What would be the best way to update the JTree when I'm changing my DataStruct with the JXTreeTable?
(JXTreeTable and JTree both use custom models)