Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Swing Component Using Validate or Revalidate

Tags:

java

swing

Whenever I remove and add swing components from say JPanel, shall I perform call on validate or revalidate?

like image 691
Cheok Yan Cheng Avatar asked May 13 '09 18:05

Cheok Yan Cheng


People also ask

What is validation in Java Swing?

validate() : In Swing when you create a Component, it is not valid i.e. its valid property is false . A component is said to be valid, when its width, height, location and stuff has been determined. This is usually done by calling their validate() method, directly or indirectly.

How do you repaint a swing in Java?

In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint(). repaint(): This method cannot be overridden. It controls the update() -> paint() cycle.

What does revalidate () do in Java?

revalidate(): This method tells the layout manager to recalculate the layout that is necessary when adding components.


4 Answers

revalidate() would be better. revalidate() marks all the container upto the top level as not proper or not valid. Then it calls validate() on the top level. The validate() method of the parent checks if at least one of its immediate children is signaled invalid or improper. it calls validate of the parent. so calling revalidate() automatically means calling validate().

like image 119
sazamsk Avatar answered Sep 22 '22 01:09

sazamsk


revalidate() is basically a invalidate() followed by a validate().

Look at Sun's Java source code.

You want to call revalidate().

like image 39
Avrom Avatar answered Sep 22 '22 01:09

Avrom


At least in Java 7, revalidate() doesn't necessarily "erase" removed components from the screen. I believe that happens when the bounding box shrinks. For these cases, call repaint() after the revalidate().

like image 28
htimesh Avatar answered Sep 23 '22 01:09

htimesh


I would think revalidate() is what you want. The validate() method will be automatically called for you after a call to revalidate(). See the Java API for JComponent.revalidate().

like image 39
Nicholas Hirras Avatar answered Sep 19 '22 01:09

Nicholas Hirras