What's the difference between Component.isShowing()
and Component.isDisplayable()
? I want to use them to decide wheter I should stop/start a Timer.
-
which part exactly of the api doc don't you understand?– kleopatraCommented Aug 15, 2012 at 12:27
-
you can't show JComponent couldn't be Displayable– mKorbelCommented Aug 16, 2012 at 8: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.
-
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?– keuleJCommented Aug 15, 2012 at 12:41
-
no .. you just need to check isShowing() ... isVisible() will also work ... isShowing() will never return true when isDisplayable() returns false Commented Aug 15, 2012 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!– keuleJCommented Aug 15, 2012 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.– janCommented Mar 7, 2014 at 9: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 willisVisible
,isDisplayable
andisShowing
output? Commented Aug 6, 2016 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).
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.
-
That's wrong. The Component can be visible, but the parent could be not.
isShowing()
checks the parent too,isVisible()
not.– janCommented Mar 7, 2014 at 9:14