I'm currently programming a little Java game and I use the JOptionPane class to alert the user if an IOException is thrown. I'd like to know how to prevent the option pane from being "closed" by clicking anywhere else, e.g. on the desktop, because it actually doesn't end the Java process.
If you want to have you window always visible (regardless of the other windows) there is an option called alwaysOnTop, when you set that to true, the host OS will try to honour that directive displaying the window always.
Try to see if these posts helps:
JOptionPane displays behind the parent JFrame
how to show JOptionPane on the top of all windows
Related
I'm using Windows builder for create an application in Java. I create a frame with the login interface. What i want is that if the user insert correct information he will write something.
I don't want to open another JFrame I would like that the Login frame will be substitute with another one in order to have only one windows.
Could you tell me the correct object that I must use?
The best options are using either a JOptionPane or a JDialog
JOptionPane works as a message box, and can be customized to your linking and usage. For example, if what you wanna show is a sucessfully logged in message, you could use:
JOptionPane.ShowMessageDialog(null, "Logged in sucessfully", "Logged in", JOptionPane.INFORMATION_MESSAGE);
You can find out about each parameter here. It's a pretty complete documentation of the JOptionPane.
JDialog is another time of window used by Window Builder, and is works as a window that cannot be switched, a classic modal dialog. While this screen is in usage you cannot acess others (as your JFrame), unless you command it to close. And to use this you can simply create it as you would with your Frame. It's modality is set by default.
From what I get of your message the first option would be the best alternative, but that decision's up to you.
I highly recommend you to look into some Window Builder documentation before you'd resort to Stack Overflow. This would be helpfull, it explains everything you need to know about the usage of Window Builder's windows and it's funcionalities.
you can close your current frame by using the command
this.dispose();
this will just close your current window. Just make sure you open the other window too.
JFrame frame = new [yourClassname]();
that should work just fine :)
I created a swing application on Windows OS. One of my JDialog (whose window is parentJFrame) shows a JOptionPane.
JOptionPane.showMessageDialog(parentJFrame, "I am a JOption"); .
At the run time, the parentJFrame setAlwaysOnTop(true) . Even though it has set alwaysOnTop-TRUE, the JOptionPane appeares on the parentJFrame on Windows OS. but When I ran it on Linux OS,JOptionPane displays behind the parentJFrame.(May be the reason is that parentJFrame alwyasOnTop is true, but how JRE runs the same application in different ways for diffrent OS s ?) How can I get it on the top of parentJFrame in Linux.? This is urgent please..
When a 'alwaysOnTop-true' component shows a JOptionPane, JOptionPane appears behind the component in Linux.. :( . But this handled well in Windows OS. JOptionPane is showed on the top of the component which is set 'alwaysOnTop-true' . It seemed that, there is a conflict Showing components on the desktop screen in Linux OS.. I m not sure so. But I guess it.
Linux OS has Oracle JDK and JRE 7
The behaviour you see on Linux is in accordance with the API specification. This is what it says for Window.setAlwaysOnTop():
If there are multiple always-on-top windows, their relative order is unspecified and platform dependent.
And also:
All windows owned by an always-on-top window inherit this state and automatically become always-on-top.
Which would explain why the JDialog that's at the heart of JOptionPane also has "always on top" status. Seems that on Windows by chance it works as you expected, but really you're asking Swing to do something impossible: To show the parent "always above other windows", but also to show the dialog on top of it.
Here's a possible workaround: Place the dialog next to the parent, so that while it's under it on the z-axis, the user will still see it:
JDialog dialog = new JOptionPane("Message").createDialog(parent, "Title");
Point dialogLoc = dialog.getLocation();
Point parentLoc = parent.getLocation();
dialog.setLocation(parentLoc.x + parent.getWidth(), dialogLoc.y);
dialog.setVisible(true);
Do note that there is no single "Linux OS", especially when it comes to window management - there are lots of different desktop environments and window managers that behave in widely different ways when it comes to window ordering and visibility, often deliberately.
This is very Simple : write this line of code after the code which you want to show ddialog box:
JOptionPane optionPane = new JOptionPane("Reports are Generated");
JDialog dialog = optionPane.createDialog("Success!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
Dont change anything exept Strings in double quotes.
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 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.