Difference between JComponent.isShowing() and isDisplayable() - java

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

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.

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.

Related

isShowing() vs isDisplayable()

What is better to use:
.isDisplayable()
or:
isShowing()
to check if JButtons are visible, but also for other things? Also what is the difference between them? At this moment they look like same thing to me, but i'm a beginner.
they are giving same results, even when button was set to Opaque(false)
Don't confuse opaqueness of a component with visibility of a component.
Opaqueness is for painting the background of the component.
The component can still be visible even if the background is transparent.
Try using setVisible(false) to see the difference between the two methods.
A component can only be showing if it is:
displayable
visible

What's the difference between setFocusable() and setFocusableWindowState()?

I am confused about these 2 APIs:
java.awt.Component.setFocusable():
Sets the focusable state of this Component to the specified value.
This value overrides the Component's default focusability.
And:
java.awt.Window.setFocusableWindowState():
Sets whether this Window can become the focused Window if it meets the
other requirements outlined in isFocusableWindow. If this Window's
focusable Window state is set to false, then isFocusableWindow will
return false. If this Window's focusable Window state is set to true,
then isFocusableWindow may return true or false depending upon the
other requirements which must be met in order for a Window to be
focusable.
I would say:
Component.setFocusable() is for individual components.
Window.setFocusableWindowState() is for the entire window. If a window can't be focusable then none of the components can be focusable by default either. This would also be used by the OS. For example, when you use Alt-Tab on Windows to cycle through the windows any window that is not focusable will not be reachable.

Does isVisible() guarantees the visibility of the UI object in JAVA

Its generic question. If I add some UI objects such as JButton etc and check the isVisible property, it would return true. However, does it guarantee that the object is actually rendered and visible correctly on the display? If not, is there some better way to check the same?
No isVisible() is only a hint for this component itself. If it is really visible for the user depends on the parent hierarchy of the component. This can be verified best by isShowing() inherited by Component, which checks isVisible() and isDisplayable() of the component and all it's parent components. But as the javadoc mentions even that does not really guarantee that the component is really really visible to the user:
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.

SetFocusable method or Focusing Components Java

I came across this code:
public class Board extends JPanel implements ActionListener{
public Board() {
setFocusable(true);
}
}
What exactly does setFocusable(true) do to the JPanel object? What is the notion of a component being focused?
Based on the Java API, this method is located in the Component class, the super class of JPanel. The method description states "Sets the focusable state of this Component to the specified value. This value overrides the Component's default focusability." This description sounds way too technical and high-level jargon for me (who just finished a Java class in the summer). Sometimes, I think these method descriptions were not written for all people with different levels of knowledge of Java. May someone explain the method description in layman's terms?
The focusable flag indicates whether a component can gain the focus if it is requested to do so. The JPanel component is focusable by default, so nothing will be changed when you set it to true.
A component that is not focusable can not gain the focus.
An example
Let's say you have implemented a dialog with several text fields and you want the user to enter some text. When the user starts typing, one text field needs to have the focus of the application: it will be the field that receives the keyboard input.
When you implement a focus traversal (a convenient way for the user to jump from one text field to the next, for example by using the tab button), the user can "jump" to the next text field. The application will try to gain the focus for the next field to prepare it to receive text. When the next field is not focusable, this request will be denied and the next field will be tested. For example, you wouldn't want a label to get the focus because you cannot enter text into it.
The focusable flag is set to true by default in the Component class. When you construct an object derived from the Component class (for example, when you construct your JPanel), the constructor of the Component class is called and sets the default focusable flag to true.
Derived classes that wish to override this default can call the method setFocusable to change that default, like you did in your example.
Note that setFocusable does not set the focus in itself, it just gives the ability to potentially gain the focus to the component.
You can use setFocusable(boolean n), it´s mainly used to activate or deactivate the focus event (component of the graphical user interface that is selected to receive the input) of the view, both in the tactile / mouse mode, and in the keyboard (cursor) mode.
setFocusable() is actually a method from the Component class in Swing.
public void setFocusable(boolean focusable)
It lets the component (in your case, JPanel which extends Component) have the power of getting focused. It doesn't actually set the component to be focused, it just indicates if the component can be focused or not, which is determined by the boolean parameter passed.

What is parent Component for in JFolderChooser.showOpenDialog

Case 1:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component
Case 2:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);
What is the practical difference between the two cases?
Checkout the Javadoc for JFileChooser
The parent argument determines two
things: the frame on which the open
dialog depends and the component whose
position the look and feel should
consider when placing the dialog. If
the parent is a Frame object (such as
a JFrame) then the dialog depends on
the frame and the look and feel
positions the dialog relative to the
frame (for example, centered over the
frame). If the parent is a component,
then the dialog depends on the frame
containing the component, and is
positioned relative to the component
(for example, centered over the
component). If the parent is null,
then the dialog depends on no visible
window, and it's placed in a
look-and-feel-dependent position such
as the center of the screen.
internally it tries to get a window using the parent using this JOptionPane.getWindowForComponent(parent). Which in turn checks if parent is null or not...
if (parentComponent == null)
return getRootFrame();
If it is null then Root level frame is returned as parent container.
Using the internal SwingUtilities.getSharedOwnerFrame(). The javadoc for SwingUtilities.getSharedOwnerFrame() says...
Returns a toolkit-private, shared,
invisible Frame to be the owner for
JDialogs and JWindows created with
null owners.
You can specify the parent to determine which component the dialog is related to. It will determine the position of your dialog (centered, relative to the parent). I also guess that the dialog will be modal, thus blocking the parent window.
If you specify null, the dialog shown won't belong to any component, and I guess it will be displayed either at the top left of the screen or at the center (the last being more likely to happen, I have not tested).
Hop this helps !

Categories

Resources