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

- 334,321
- 69
- 703
- 674

- 3,418
- 4
- 30
- 51
-
which part exactly of the api doc don't you understand? – kleopatra Aug 15 '12 at 12:27
-
you can't show JComponent couldn't be Displayable – mKorbel Aug 16 '12 at 08:04
3 Answers
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.

- 32,039
- 22
- 142
- 171

- 6,170
- 1
- 18
- 23
-
So, if I want to check if the Component is actually visible for the user, I can use isShowing()? Or is it possible that isShowing() returns true and isDisplayable() returns false? – keuleJ Aug 15 '12 at 12:41
-
no .. you just need to check isShowing() ... isVisible() will also work ... isShowing() will never return true when isDisplayable() returns false – Pr0gr4mm3r Aug 15 '12 at 12:44
-
Well isVisible() could return true even if isDisplayable() would return false and the Component is not in a containment hierarchy anymore... I'll go with isShowing() then. Thanks! – keuleJ Aug 15 '12 at 21:12
-
I think it's important to note the following javadoc of Components `isShowing()` method: Note: sometimes there is no way to detect whether the Component is actually visible to the user. This can happen when: - the component has been added to a visible ScrollPane but the Component is not currently in the scroll pane's view port. - the Component is obscured by another Component or Container. – jan Mar 07 '14 at 09:11
-
**Important Question:** What happens if the component is `setVisible(true)` but the bounds of the component are outside the JPanel so the user can't actually see it. What will `isVisible`, `isDisplayable` and `isShowing` output? – Lakshya Goyal Aug 06 '16 at 10:10
isDisplayable() returns true iff the component's peer is not null (the peer is the native window container).
isShowing() returns true if the component is visible (i.e. setVisible(true) or show(true) was called), its peer is non-null, and if it also has a parent, the parent is also showing (i.e. isShowing() on the parent returns true).

- 402
- 3
- 9
As far as I understand Component.isShowing() returns true if the component is visible and Component.isDisplayable() returns true if the component is in displayable hierarchy and that means it can be displayed. I think methods names speak for them itself.

- 13,799
- 21
- 74
- 110
-
That's wrong. The Component can be visible, but the parent could be not. `isShowing()` checks the parent too,`isVisible()` not. – jan Mar 07 '14 at 09:14