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

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.

Related

JavaFX (Windows): Undecorated window isIconified not updated when un-minimized

I have made an undecorated window, and I have used this answer as a reference in order to make it minimizable when the user clicks on the taskbar icon. I have mainly used the "simplest" version in the answer, and it is working.
Recently, I have discovered one problem; although I can minimize/un-minimize the window by clicking on the taskbar icon, but the iconifiedProperty does not change its value when the window is un-minimized - the Stage needs to receive another layout change (e.g. setMaximized(true)) in order for iconifiedProperty to transit to false state.
I need to detect this change because I am also making the window not to be full-screen when it is maximized (See this bug, it includes a temporary workaround). Again, I can make the size of the maximized window take the full visual bounds of the screen, but when a maximized window is minimized and subsequently un-mininized, the window will revert to covering the taskbar (full-screen).
Any help would be appreciated.
Edit
An update on the problem. I just realized that iconifiedProperty does not change only when the window is in maximized state.
I.e.
Window is at maximized state.
Window is minimized (no matter via setIconified(true) or click of taskbar icon).
Window is un-minimized by clicking on taskbar icon.
This will cause iconifiedProperty to transit to true in step 2, and will not go back to false in step 3. Thereafter, repeating step 2 and 3 will not cause in the change of this property either.
This problem is not observed to happen if the window is not in maximized state, though.
Edit 2
Further testing shows that it has nothing to do with being undecorated - the iconified property change does not happen even on a normal maximized window.

How to create a common static focusable window in Java?

I am creating a Transliterating tool in Java. It's almost complete.
Here is the screenshot.
I am using JWindow for dropdown, which must be focusable for some reason.
As, user can write in one input only at one time. I've created this window static, so all Text components use the same instance instead of creating new one.
Problem occurs, when I work in more than one window. It works fine unless both window are showing on screen. But when owner window of this dropdown window is closed, dropdown window is not focusable anymore.
As Javadoc of JWindow(Window owner) constructor says:
Creates a window with the specified owner window. This window will not be focusable unless its owner is showing on the screen. If owner is null, the shared owner will be used and this window will not be focusable.
So, how can I create a static, focusable window, that is shared by all components in different windows.
Don't use a JWindow.
Instead you can use a non decorated JDialog. Then you won't have the focus problem.
Edit:
You can prevent the dialog from initially getting focus when you make it visible by using code like:
dialog.setWindowFocusableState(false);
dialog.setVisible(true);
dialog.setWindowFocusableState(true);

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.

Difference between JComponent.isShowing() and isDisplayable()

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.

What does focus means?

Just wondering what focus means in java code, because I have seen onWindowFocusChanged, addFocussables, findFocus...
If I have a scrollable list and I scrolled it down, the first item will have focus false? or it means other thing?
Thanks
Focus means you have selected the particular GUI element. For example when you select a window that window gains focus, when you select another window the first window loses focus.... It's the same for JTextField, JTextArea, etc.
The definition of the focus here on StackOverflow is as follows:
Focus indicates the component of the graphical user interface which is
currently selected to receive input.
Saying that focused component is selected is not accurate. For instance, we can have a JCheckBox which is deselected (has no tick mark) and it is also the current focus owner. Since it has focus, its state is toggled with the spacebar. The term active is more precise. I came up with the following definition of focus:
Focus is a state of a component in which it receives keyboard input. Focus is represented by some visual cue; for instance, in Metal look and feel a focused JButton has a blue rectangle around its label. The component with the current input focus is called the focus owner.
The current GUI element that is "active" has the focus. For example when you have several Input windows only one can have the focus and receive your keyboard input. See here the Android GUI doc http://developer.android.com/guide/topics/ui/ui-events.html

Categories

Resources