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.
Related
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
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.
I'm trying to understand when to use revalidate/repaint/pack.
Surprisingly I haven't found much detailed under-the-hood documentation (feel free to link).
So far I have understood that this is all the responsibility of the RepaintManager.
paint/repaint refer to what sees as dirty/clean
pack/validate/revalidate refer to what is valid
This article on Oracle explains that calling a repaint enqueues a job on the Event Dispatcher Thread that will in turn call paintImmediately() on the component (this redraws the component).
This trail indicates that to be or not to be valid is associated with the LayoutManager. And that this is all about the size of the component rather than the content.
Is it true that you should call revalidate when you move/resize your component and repaint when you change it's contents?
Is the pack() method really a deprecated thing that you should never call?
Are any of the above claims wrong?
Here are a few basic cases where you need to invoke those methods (I cover the basics but I may have missed a few other cases where calling those methods would be required).
You should call revalidate() on a container when you have either: added one or more component, removed one or more components, changed the constraints of one or more contained components (constraints or XXXSize(), although the latter is not recommended), changed the LayoutManager of the container.
You should call repaint() whenever you want that component (and its descendants) to be repainted. Eventually, this will call paintComponent() (for basic widgets this will delegate to XXXUI.paint()), paintBorder() and paintChildren() (at least in Swing)
pack() actually sets the size of a window to its preferred size. You should usually call this right before making the window visible. You may call it later on but this will give a weird user experience (I don't know many applications that resize their windows once displayed)
The main advantage of using revalidate() and repaint() is that they can coalesce themselves. This means that if you call several times repaint(), the painting will only be performed once.
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.
There seems to be many methods in Java awt Container class that are related to validate. Apparently they don't do data validation. Is it useful for a Swing developer in any cases? Some of the methods:
validate(), invalidate(), validateTree(), isValid() etc.
Citing the API doc:
The validate method is used to cause a
container to lay out its subcomponents
again. It should be invoked when this
container's subcomponents are modified
(added to or removed from the
container, or layout-related
information changed) after the
container has been displayed.
Validation in a Swing context concerns requesting a component to lay-out its sub-components after one of these is modified.
For example, suppose you implement a custom JDialog with a button "Show Filters". Upon clicking this button, you might want to add an additional "filter" panel to the south of the JDialog. Upon adding the new sub-panel you would be required to call validate() on the JDialog to cause it to lay-out the new panel correctly.