Search code examples
javaswingjcomponent

setVisible() efficiency


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)
   }

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.