0

Having read many tutorials, articles and questions, I am still have confusions about updating the GUI. Plus there are numerous related questions here on this website and still no luck - even though I think my problem is very simple.

Basically, I have a JFrame that has a JLayeredPane as its root container. And I have some layers of JPanels inside it.

The main issue is with updating a particular JPanel in this JLayeredPane. And for this particular Panel, I have implemented an update method that changes the contents inside it.

updatePanel(int para)
//doesn't remove this panel
//removes some existing labels and replaces it with new ones

Once I create the whole Frame, obviously just calling this method won't show any change displayed the frame.

private void static main (String[] args){
    WindowFrame frame = new WindowFrame()//WindowFrame extends JFrame
    frame.updatePanel(2);
    .....
    .....
}

And that's where I am stuck. I want to update the contents as the frame is displayed.

I saw these methods mentioned by people but due to nature of problems, I couldn't fully grasped the concepts. Plus the documentation on these methods isn't really helping - at least to me.

revalidate()
validate()
repaint()

How/when should these methods should be called? Or is this not the right way of what I should be doing, given these methods and the problem I am trying to solve?

Thank you for your time.

jbchichoko
  • 1,574
  • 2
  • 12
  • 16
  • Question and answer http://stackoverflow.com/questions/9885855/how-to-dynamically-add-jbutton-to-jpanel/9885927#9885927 might be useful – Robin Apr 19 '12 at 06:22

2 Answers2

2

Basically you need two methods:

revalidate()

This method does the same as invalidate() but in AWT event dispatching thread (i will just call it Swing thread later on)). It updates container and all of its ancestors (parent containers in which this one is placed) layouting.

Basically if you either move something inside this container or place/remove components inside of it you should call this method (or invalidate in case you are performing it in Swing thread, for example inside any Mouse/Action listener body or just inside).

repaint()

This method forces component, all its sub-components (if it has them) and parent container (basically if this component is NOT opaque) to update what they are "painting".

Usually you don't need this method since all standard Swing components know when to repaint themselves and they do it on their own (that ofcourse depends on components UIs and some other things). This method might be useful in case you have your own specific components with some unique painting-way (for e.g. some custom selection over the components) and in some rare problematic cases with standard components.

Also the way this method acts depends on the components placement (due to some Swing painting optimizations) - if you have some massive repaints rolling you'd better optimize them to repaint only those parts (rects) that you actually need to repaint. For example if you change the component bounds inside any container the best choice is either to repaint its old bounds rect and new bounds rect OR repaint rect that contains both of those bounds, but not the whole container to avoid repainting uninvolved in the action components.

So, basically in your case after some changes with panels you should call revalidate on their container (or invalidate) followed by repaint (in case revalidate leaves some visual artefacts) again for the container.

Guess i didn't miss anything and i hope that now you know the basic meaning of those methods.

Mikle Garin
  • 10,083
  • 37
  • 59
0

revalidate at the end of your update method like so .

updatePanel(int para){
 .....
 .....
 this.revalidate();  //of course this refer to the panel
 parent.revalidate(); // parent refer to the window

 }
evanxg852000
  • 249
  • 3
  • 6