Specifying the target screen of a lone JOptionPane - java

I have an application that uses the JOptionPane.show* methods to inform the
user about various conditions before displaying the applications main window.
On a multi screen setup these always show on the first screen. This is usually only
a minor annoyance, but becomes a problem when the 0 screen is off or disconnected.
Normal windows can be placed correctly using the GraphicsConfiguration obtained
via MouseInfo, but I can't find a way to pass that to JOptionPane. I can
not either use the main window to anchor the dialogs, because there is no main
window at that stage of the application startup. Among the possible dialogs is
a warning about obsolete java versions, so displaying the main window before the
errors is not option since the user's java runtime may not even be capable of
displaying the main window.
Is there a way to specify the target screen without reimplementing a major part
of JOptionPane?

You can create a JDialog out of a JOptionPane, and then display the dialog any location that you desire.
As per the JOptoinPane API:
JOptionPane pane = new JOptionPane(arguments);
pane.set.Xxxx(...); // Configure
JDialog dialog = pane.createDialog(parentComponent, title);
// here set the dialog's location
dialog.setVisible(true);
Edit: Alternatively, you could simply create your own JDialog window de novo as per Andrew's great comment (that now no longer exists?).

Related

Java Swing JDialog - how to modify after it is displayed? [duplicate]

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

WindowActivated and WindowGainedFocus [duplicate]

What's the main difference between the method windowActivated (implemented from WindowListener) and windowGainedFocus (implemented from WindowFocusListener)?
The Java documentaion says:
windowGainedFocus:
Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, will receive keyboard events.
windowActivated:
Invoked when the Window is set to be the active Window. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.
But what's the difference? Or is it just as it says, that a focused window is a type of activated window?
Thanks in advance!
From How to Write Window Listeners which reflects the quote in your question aswell:
windowActivated(WindowEvent) and windowDeactivated(WindowEvent):
Called just after the listened-to window is activated or deactivated,
respectively. These methods are not sent to windows that are not
frames or dialogs. For this reason, the windowGainedFocus and
windowLostFocus methods to determine when a window gains or loses the
focus are preferred.
So windowActivated is only executed when the window is a frame or dialog, while the windowGainedFocus is for all types.
The focused window is the one that receives keyboard input.
An active window is typically the document window that the user is manipulating.
An active window is generally distinguished visually, for example, with a different title bar.
In macOS, the focused window is called the key window and the active window (there can be only one) is called the main window.
The distinction is subtle because they are almost always the same window.
An example where they differ would be a floating palette that contains a text field. The palette would need to be the focused window to accept keyboard input, but the document window is the active window where the changes are actually made and it should be distinguished from other (inactive) document windows.
Although Java distinguishes active and focused windows in its API, the implementation links them together so that some combinations (like the above example) are not possible, or at least difficult to arrange. For example, if you click on a focusable Java window, it becomes both the focused window and the active window.

Dynamically show and hide JFrame decorations

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

Java Swing - user alerts

I am trying to build a user alert mechanism by bringing up the window to the front and then flashing the icon on the screen for the user. I have two questions with regards to this approach:
How can you find the current window you are at in Java and then de-minimize it and bring to front?
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
Thanks a lot for any replies.
How can you find the current window you are at in Java and then de-minimize it and bring to front
Window[] allWindows = Window.getWindows();
returns arrays of all Top-Level Containers from current JVM e.g. J/Frame, J/Dialog(JOptionPane), J/Window,
you can to test for (example) if (allWindows[i] instanceof JFrame) {
then WindowState returned WindowEvent
by bringing up the window to the front and then flashing the icon on the screen for the user
use undecodated JDialog (works toFront, toBack) with
create only once time
setDefaultCloseOperations(HIDE_ON_CLOSE)
use Swing Timer for hide JDialog
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
have look at Java Translucent Window, put there Icon to the JLabel (or to the JButton)
use Swing Timer for flashing by hiding Icon or swithing bewtween two or more Icons (three or four is good)
I think the simplest way to get the window ancestor is :
SwingUtilities.getWindowAncestor(yourComponent);

Why doesn't the JDialog constructor center itself over the specified owner component?

Using this JDialog constructor, where I specify the owning JFrame instance, I find that the JDialog is not centered over it's owner component. Instead, it appears in the top-left corner. In order to get this to work, I must specify the owner component in the setLocationRelativeTo method.
Why is this?
Work Environment:
Dual monitors
Windows XP OS
JDK 1.6.0_29
Note that for the JFrame instance, I use setLocationRelativeTo(null).
JDialog is very general I think. If you want quick ways to pop a general dialog box then look at JOptionPane. It has methods to easily create a centred JDialog component or immediately pop up a blocking dialog window.
e.g.
JDialog dialog = new JOptionPane("message", JOptionPane.INFORMATION_MESSAGE)
.createDialog(jFrameOwner, "window title");
Though you probably really want to look at the JOptionPane.showXxxDialog static methods. Very useful and convenient.
And you may wish to take a look at the dialog tutorial. All the dialogs produced by the java web start application can be produced using the JOptionPane class.
Sounds like a design decision. Sometimes you want to give the dialog a reference to it's parent, without to center the location over it.

Categories