Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree refresh, with new TreeModel  RSS feed

 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone.

Im trying to create my new JTree component with fallowing functions:

- reading and showing files from system directory (works)

- showing popup with options after right mouse click at tree node (works)

- added first option to the popup, collapse and expand (works)

---------------------------------------------------------------------------

Now, I would like to add options like create new file/delete file or directory or rename.

Those functions are simple, but one thing is NOT simple for me.

Its obvious that I should refresh/reload my tree after adding/deleting file or renamming it, but..

I found sollution somewhere how to reload my node (update)



There aren't any errors in eclipse, but while testing it, and for example using this function, i got error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: eu.matim.editor.sandbox.jtree.FileSelectorModel cannot be cast to javax.swing.tree.DefaultTreeModel
at eu.matim.editor.sandbox.jtree.FilePreviewer$4.actionPerformed(FilePreviewer.java:115)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


That i cant cast my FileSelectorModel to the DefaultTreeModel.

My FileSelectorModel code is here:



It would be great if someone can help me a bit how to reload my tree or node.

Thanks in advance!
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote:
Its obvious that I should refresh/reload my tree after adding/deleting file or renamming it, but..


No. Manual refresh of a component is always wrong in Swing. If your model is working correctly, you don't have to care for this; JTree does it all for you.

Mathew Mintalm wrote:



Hm... maybe these methods do serve some purpose?

TreeModel and TreeNodes are not the easiest to use components in Swing. Have a look at the DefaultXxxModel and AbstractXxxMode implementations to see how they work. And use them, especially the AbstractXxxXxx classes are a good base for own work. If you didn't do much with Swing up to now, begin with the ListModel and the TableModel. Same principles, but easier to build and understand. With JTree you typically need to implement an own node class that implements notifying correctly.

But remember rule one when working with Swing: If you are updating your component (or model) manually, you made a mistake. It should all be event driven.

E.g. if you alter a node, it should fire it's listeners itself, and that is all. If there is more interaction needed, rethink it.

Well... in reality number one rule is: Any changes go in the EDT.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answer.

Anyway i was trying implementing ListModel etc, but without any results.

I guess I don't really understand what exaclty you meant.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote:Thanks for answer.

Anyway i was trying implementing ListModel etc, but without any results.

I guess I don't really understand what exaclty you meant.


Implementing a ListModel is easier to do than implementing a TreeModel; there are more traps. But the concepts are the same. So for learning how to implement a JTree/TreeModel I would recommend to successfully implement a ListModel first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!