0

This is probably a quick win for someone very familiar with the Java Swing API. I was looking at the Container documentation here, and I got a feel that the invalidate() and validate() methods are somewhat like a constructor and destructor helper methods (if you are to use a C++ analogy). invalidate() seems like it is used to do cleanup stuff (setting your member variables to null, closing your clients, etc.) while validate() is called before creating a component.

My question: When should one leverage these two methods? What would be a really good example of the need to drive the necessity to override one/both of these methods?

Any/all help is welcome. Thank you!

Nishant Kelkar
  • 302
  • 3
  • 19
  • Basically, no. The both provide support to the layout manager API. `invalidate` will invalidate any instances of `LayoutManager2`, which can cause them do discard any possible cached values whereas `validate` triggers the layout manager process, where all the decisions required to layout the container hierarchy are made. `validate` may invalidated child components. – MadProgrammer Sep 04 '14 at 06:00
  • Agreed. But what would be a good use case (it's ok if you have a made up example) to override these two methods? – Nishant Kelkar Sep 04 '14 at 06:01
  • 2
    You are required to call them if you make structural changes to the GUI **after** it has been displayed. E.g. you remove a `JButton` from a `JPanel` after it has been displayed. Of if you change the displayed components in a way that would effect their layout, e.g. you change their sizes. But examples are mentioned in the linked thread, please read that first. – icza Sep 04 '14 at 06:03
  • Basically, they deal with any scenario where the layout or child layouts may have changed and the layout requirements for the container should be recalculated. – MadProgrammer Sep 04 '14 at 06:07

0 Answers0