-1

i create a little program that load a frame in which i added some panels. When i click on some button it should show some panels and hide other. I'm experiencing some difficult to do it, even because i don't really figure out the diference between setVisible(true), repaint() and validate() (that some friends of mine suggested to me).

I hope you can make me to understand!

Thank you.

Sorter
  • 8,916
  • 5
  • 57
  • 69
smartmouse
  • 12,988
  • 30
  • 88
  • 157
  • Are you referring to revalidate()? http://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint – Sorter Jan 28 '14 at 16:15
  • I didn't know revalidate(), anyway i tried to use it and it doesn't work. I have also have a look at your link, but it don't help me. I have this method: [link](http://pastebin.com/RM1LLd0v) Everytime i call the method the "list" content is different but the JList shows always the same content. – smartmouse Jan 28 '14 at 16:19

2 Answers2

1

Carefully read the API for JComponent. The usages are:

  1. setVisible - it will hide or show your component altogether. If you set it as false, you won't see it at all.
  2. repaint() - is called when the actual pixels need to be redrawn, this is done AUTOMATICALLY. It's used, for example, when you move a window on top of your GUI and then move it away. The part that was covered needs to be redrawan.
  3. validate() - you should call this when the layout of your GUI has changed and you need the manager to replace and redraw your GUI.

It's a bit more complicated than that, so again, carefully read the API.

rhobincu
  • 856
  • 1
  • 6
  • 19
  • I have this method: [link](http://pastebin.com/RM1LLd0v) Everytime i call the method the "list" content is different but the JList shows always the same content. – smartmouse Jan 28 '14 at 16:27
  • @smartmouse I don't see a method list in what you linked. If the content of a JList is not updated, then you're probably not updating the data model. – rhobincu Jan 28 '14 at 16:30
  • Yes, it is updated. I solved removing with this.add(viewPanel) after viewPanel.setVisiblue(true). – smartmouse Jan 28 '14 at 17:20
0

setVisible(true): sets the component so that it's visible.

repaint(): calls the paint method on the component.

revalidate(): updates the component based on the root component

Someone
  • 531
  • 3
  • 13