Question

I was wondering, would it be more efficient to do something like this:

   setVisible(false) // if the component is invisible

or like this:

   if(isVisible()){
      setVisible(false)
   }
Était-ce utile?

La solution

This has nothing to do with efficiency. Use the first one, it's simpler and probably already contains the visibility check inside the setVisible() method.

When you write code, don't try to think about efficiency, especially in ridiculously trivial cases like this. You're running on a multi GHz computer, so you're only wasting your time on micro-optimization like this.

Autres conseils

setVisible() as implemented by JComponent already calls isVisible() so it's not more efficient to add a redundant call. In any case, it would be really improbable that it would be any kind of a performance bottle neck.

The only case in which your second example would make sense, is in a case of switching visibility on and off.

if(isVisible()){
   setVisible(false);
}else{
   setVisible(true);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top