views:

239

answers:

2

Hi,

I need to change tree nodes color when business logic updates model, wait 1 second, and then change its color back.

Basically I would like to create similar thing like here http://stackoverflow.com/questions/1664924/changing-jlist-row-color-at-runtime, but for JTree.

So I could use a similar technique,but I also wont to change ancestor nodes color, so user could see notification even if not is not expanded.

How can I do it?

+2  A: 

First, when the business model is updated, make sure you fire the appropriate tree updated event in your tree model so the JTree will know to update.

Then you'll need to define a TreeCellRenderer that sets the color. When the business model is updated, tell your renderer which nodes to highlight.

However, the trick here is turning the color off. You can define a Thread or better yet an Executor to sleep for 1000 millis then tells the renderer to no longer highlight the nodes, then fire the tree update events again so the JTree will repaint appropriately.

Scott Stanchfield
You should not define an arbitrary thread or Executor to fire the tree update event as this should be fired on the Event Dispatch Thread.
Adamski
True. Instead use SwingWorker or SwingWorker + Executor
jitter
That could work, or use SwingUtilities.invokeLater() to put the tree update in the UI thread.
Scott Stanchfield
+1  A: 

Check this Highlight a node's descendants in JTree

You could easily modify it to instead highlight the parent and now you only need to plug in a solution for the timed highlighting like the solution I provided on the other thread. Highlight, timer, unset highlight.

Using SwingWorker plus maybe an Executor if the updates to the model are frequent and you want some control over the execution of the highlighting threads

jitter