0

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.

AlexElin
  • 1,044
  • 14
  • 23
smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • 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
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. – Gilbert Le Blanc Jun 12 '22 at 04:42

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 redrawn.
  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.

cottontail
  • 10,268
  • 18
  • 50
  • 51
rhobincu
  • 906
  • 1
  • 7
  • 22
  • 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
  • 551
  • 3
  • 14