问题
What's the difference between Component.isShowing() and Component.isDisplayable()? I want to use them to decide wheter I should stop/start a Timer.
回答1:
A componentisShowing()
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.
回答2:
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).
回答3:
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.
来源:https://stackoverflow.com/questions/11968780/difference-between-jcomponent-isshowing-and-isdisplayable