I want a function that I can't find wish allow to activate a window, without showing it.
If this window is a subclass of java.awt.Window you can call setVisible to hide it.
Finally found the answer:
a minimized window can't be active, and there is a way to communcate with hidden windows (if not minimized using messages)
Related
I'm making custom dialogs that I want to pop up and disable the main shell behind it so that it cannot be clicked while the dialog is active.
My initial plan was something like as follows:
shell.setEnabled(false);
doDialogStuff();
shell.setEnabled(true);
this worked but as I close the dialog, it loses focus of the shell that was open before the dialog. I managed to sort of fix it by adding
shell.setFocus();
after the last line but this is messy and causes the screen to flicker as the window loses and then gains focus in a split second, also, it sometimes doesn't regain focus and I can't understand why :/
Is there a better way to disable the background window without it losing focus.
Thanks in advance SO peeps
You should create a custom dialog based on this tutorial.
This way you just have to set the modality of the dialog to whatever you need exactly and the dialog will take care of the rest for you.
This should be helpful as well (Javadoc of Shell):
The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The PRIMARY_MODAL style allows an instance to block input to its parent. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_MODAL style allows an instance to block input to all shells, including shells belonging to different applications.
The proper thing to do is create the dialog as a modal window. When you create the dialog's shell you should do something like
dialogShell = new Shell(mainShell, PRIMARY_MODAL | DIALOG_TRIM);
We have an application which, as its first UI action, displays a modal JDialog without a parent frame.
public LoginDialog(Frame owner, Config config, Object... params) {
super((Frame)null, true);
It unfortunately has the annoying characteristic that when it appears, although it comes to the front, it does not grab the focus.
So the user, after launching the application by double-clicking on the start menu, has to use the mouse to select the "login" dialog and type in information.
Is there a way to make this JDialog grab the focus when it appears in the front?
I've tried calls to "requestFocus" before, after and via invokeLater "during" the call to setVisible(true) - but none of these seems to have any effect.
How do we make a modal dialog grab the focus?
UPDATE: The issue was the code used to try to present a background "wait window". This window was displayed "behind" the login dialog as a hack so that when the dialog disappeared the user would see the "Please wait" message. As it was the first window created by the application, it got the focus. I am not sure if there would have been a way to make the dialog gain the focus again inside the event dispatch thread with this hack - but I fixed it by un-hacking it and doing things properly.
First, it a little strange that modal dialog is parent-less. The point in modal dialog is that it is displayed on its parent and does not allow to access parent.
So, the first recommendation is to make it non-modal. I believe it will work.
BTW I have just tried to create such dialog and have not problems with focus. Try probably to simplify your code:
JDialog d = new JDialog();
d.setSize(200, 200);
d.setVisible(true);
This works for me and I believe will work for you. Now start changing this simple code towords your real application code. At some point it will stop working and you will see where the problem is.
If nothing helps try to use the trick I described in this article. Look for title "Portable window activation". I hope it will help.
See Dialog Focus for a potential fix using a RequestFocusListener. I have used it successfully for setting focus in JOptionPane dialogs.
1) you have to create JDialog contents and showing container wrapped inside invokeLater()
or best and safiest way is
2) you have to set for ModalityTypes or Modal for parent
3) only one from containers could be Modal in applications lifecycle
I'm trying to reproduce a feature I've seen on several apps:
I have a GUI app with several JDialogs.
I'd like to easily organize them tightly on screen:
when I move one JDialog, and one of its borders gets "close" (within 5 pixels for example) to another JDialog, I'd like it to automatically snap and stick right along it.
any idea how to achieve that ?
Add a ComponentListener to the dialog and listen for the comopnentMoved() event.
You can use the Window.getWindows() method to get all the Windows. Then you loop through the Windows and get the bounds of each window. Whenever you are near a window you manually set the size of the window you are moving.
Of course you will also need to handle the situation when you want to move the window away from another window so maybe you need to start a Timer with every componentMoved event and only manually position the window after events have stopped being generated.
I have a Java network application and this is what I want to do:
After user logs out, his interface window closes.
Now, I want for another window to show up that will say something like: "Thank you for using our application".
This final window should be borderless and without any available option, more like a plain picture (jpeg? why not?). By clicking on it, user will be sure to close this final window.
I googled and couldn't fin anything on this matter, it's hard to phrase the question.
I hope someone helps me...
https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JWindow.html
A JWindow is a borderless, undecorated JFrame (no titlebar or buttons).
This should be what you need.
This should help:
http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html
You're interested in the windowClosing and windowClosed events
You have various possibilities, depending on when you want this dialog to display :
if you want it to display juste before the app closes, use addShutdownHook
if you want it to display when the last window closes, use addWindowListener
You can then use a JWindow with your image inside, and use addMouseListener to wait for the user to click on it.
I have a JApplet which is used for chat. I would like to make it possible that when the applet is minimised and a chat message is received by the user, the minimised window becomes orange (and thus shows the user that something has occurred).
How is it possible to make the applet do this?
Thanks,
Tim
You may have access to the system tray in an applet (I'm not sure). Have a look at the java.awt.SystemTray class - the in-tray lets you pop up messages to the user.
Alternatively you could attempt to cause the Window's toFront method to be called or to "maximize" using the setSize methods (again, I'm not sure what effect this has in an applet). I suspect that the toFront method will be a good bet
Another option I'd look at is raising a JDialog. The presence of this may cause the OS to draw attention to the minimized applet. You could listen to window events representing the screen un-minimizing to clear the dialog so that the user never knew it was there.