Search code examples
javaswingjcomponent

Difference between JComponent.isShowing() and isDisplayable()


What's the difference between Component.isShowing() and Component.isDisplayable()? I want to use them to decide wheter I should stop/start a Timer.


Solution

  • A component
    isShowing() when

    Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing.

    isShowing() is recursive and checks all parent components, too, but isDisplayable() and isVisible() verify only the state of the component, not that state of its parents.

    This means that your component is currently showing on the screen within a Frame, Panel, etc.
    setVisible(true) --> isShowing() returns true (in most cases)
    setVisible(false) --> isShowing() returns false (in all cases)

    isDisplayable() when

    Determines whether this component is displayable. A component is displayable when it is connected to a native screen resource.

    A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible.

    A component is made undisplayable either when it is removed from a displayable containment hierarchy or when its containment hierarchy is made undisplayable. A containment hierarchy is made undisplayable when its ancestor window is disposed.

    This means that your component is in a state where it can be shown on the screen but it doesn't need to be currently shown on the screen to be in a displayable state. E.g., even if setVisible(false) was called on the component before (so the component is "invisible") the component is still displayable and isDisplayable() will return true.