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

How to refresh my GUI component properly?  RSS feed

 
Jim Yin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all
I'm confused by GUI component refresh at long time.
There are several methods to repaint GUI, repaint(), revalidate(), validate(), update(), updateUI(), so, which method(s) should I invoke?
Such as adding/removing components at JPanel dynamically, or adding/removing nodes at JTree?
TIA
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do your work in the Swing thread (via SwingUtilities.invokeNow or SwingUtilities.invokeLater ), your gui components should update automatically.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the component
Originally posted by Jim Yin:

Such as adding/removing components at JPanel dynamically

You would invoke invalidate() on the JPanel. This tells the layout manager to rearrange the components in the JPanel and sets it up for being repainted.


adding/removing nodes at JTree?

You add or remove nodes to the JTree data model and the model should tell the JTree that the data has changed and it should update. In the javax.swing.tree.DefaultTreeModel, one invokes one of the fireTreeNodes*() methods which fires an event letting all the data model's listeners, of which JTree is one, that the data has changed.
As always, the Java Tutorial: Creating a GUI wth Swing contains valuable information and examples
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!