Why when I click on the x button to close the window in a Java application only the window dissapears and the applicaton is still running.
I've read so many times that java designers tried to cater Java behaviour for the most common needs of programmers and save they precious time and so on and so on. What's more common case than closing app when I click on a X button?
Whatever the reason is, it really doesn't matter as long as you're aware of how it works and how to make your program behave the way you want.
At a guess though: One app can have several windows, exiting the app when one of them is closed doesn't sound very smart. Keeping track of how many windows are open/closed/hidden/not yet shown and so fort to be able to exit when the last windows is closed might have been too much work/too many edge cases etc. Thus it's up to us, programmers, when we want our app to exit.
In any case, if you want your app to exit when a window(JFrame) is closed, you can just tell it to do so:
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Many applications have more than one window open at a time and closing a single window should definitely not shut down the whole application.
Also, most application want to do a bit more than a simple System.exit(0); when the quit. They might want to ask the user to save some files (or save them automatically), they might want to clean up some temporary files or notify some remote host of their shutdown or something like that.
In effect it's very unlikely that the only effect of closing a window is ending the JVM.
myJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dispose is almost always preferred. If you explicitly say EXIT_ON_CLOSE and you ever want to display more than a single window, you will need to modify your code to use DISPOSE_ON_CLOSE instead. Not a huge deal, of course, just seems a better choice.
In my opinion, the DISPOSE_ON_CLOSE should have been the default, not HIDE_ON_CLOSE. Oh well, that's what we've got. EXIT_ON_CLOSE should probably be avoided for the same reason that calling System.exit(0) should be avoided in a window listener (though there's nothing wrong with it per se, it's just not very future flexible).
Even better, it's almost always preferred to attach a WindowListener to your frame and set the default close to JFrame.DO_NOTHING_ON_CLOSE. This way, you control when your application shuts down, including a prompt to the user to save any work, etc.
Related
Is there a way to make sure that a user cannot close or leave my Swing application? I've tried to make it fullscreen, but you can still Alt-Tab away from it—and besides, that doesn't work well when you decide to use JOptionPane's dialogs.
So, is there any way to make a user use only this one Java program on a device?
Edit: Some people wonder about the purpose. The application is supposed to be sorta "embedded" into the handheld device (which runs under Windows), so the users of the device will use it as we intend it to be used—for example, that they won't play Freecells or do something worse instead of doing the actual work. Have you seen ticketing kiosks? They are locked down pretty well, you can't just close their big flashy GUI and get to the Windows desktop!
Okay #Daniel showed you how to make a Java app un-closeable (+1 to him) by calling:
JFrame#setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
on the JFrame instance.
To make sure you cant leave it i.e by pressing CTRL+ALT+DEL and ALT+TAB etc you may want to do this (applies to windows only):
1) Disable TaskManager/CTRL+ALT+DEL by:
Setting registry key:
HKCU/Software/Microsoft/Windows/CurrentVersion/Policies/System/DisableTaskMgr = 1
via reg script or cmd.exe.
2) To disable all shortcuts together like ALT+TAB etc see here (Download/use the *.reg script and execute it via cmd).
Uncloseable yes, you can use setDefaultCloseOperation(); and pass it JFrame.DO_NOTHING_ON_CLOSE
Example:
import javax.swing.*;
import java.awt.*;
class app extends JFrame {
app(String title, int height, int width)
{
super (title);
setSize(width, height);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}
}
class program {
public static void main(String[] args) {
app myApp = new app("Hello", 350, 750);
}
}
We do something similar with a POS application, but we cheat a little bit. While making use of Full Screen helps a lot, we found users could STILL exit the application.
In the end, we created a Windows Service (yes, we run on Windows) that automatically RESTARTS as soon as it's closed. So, you can see the Windows desktop (we removed the icons) for a split second, but then the app pops up again. The benefit of this is also that we can update the JAR file remotely, from the intranet, and all the users need to do is push a button that closes the system, it restarts automatically and is updated. We wanted to use WebStart, but had problems integrating it with our wrapper.
The wrapper itself is a Python application that just starts the JVM and application, compiled to an EXE. Simple, but effective.
The net effect is a closable application that will automatically start itself again. Good enough for our use where we need to user to be able to do one thing and one thing only. Giving the startup credentials for the application Admin privileges, and the users just a normal account, took care of the pesky 'alt-ctrl-del' users as well. You can't kill a process that's got higher access rights than yourself.
If you don't feel like writing your own wrapper, give http://wrapper.tanukisoftware.com/doc/english/product-overview.html a whirl, it looks like a brilliant product.
You can catch pretty much any keystrokes you like (and have them ignored) in your app, so Alt-Tab can be fixed. The one key combination that an application can never (ish) handle itself, is the Ctrl+Alt+Delete, which is hooked into the kernel in many operating systems for security reasons. As a side note: this is why many login screens ask you to hit C+A+D before entering your username and password.
You can make a maximized window that can't be unmaximized or closed. You'd also have to trap certain keystrokes such as CTRL+SHIFT+ESCAPE, ALT+TAB, WIN+[anything], various Fn keys, and you'd have to do something to prevent CTRL+ALT+DELETE from working (Either from showing the task manager or from bringing up the blue options screen in windows vista or 7.
I believe there are windows API calls that can change the behavior of CTRL+ALT+DELETE somehow but have no idea what they are. A good place to investigate would be Sysinternals Process Explorer, which has the functionality to replace the task manager. Figure out how it does this, then replicate it.
I making a webcrawler and I only need the GUI to set parameters for the crawling, after that, the execution runs forever in an infinite loop.
It's possible when I start crawling (currently clickin on a button), I dispose of the GUI to free resources and and run the application in text/console mode?
You should be able to close your application's frame. Just make sure that the frame isn't set to exit on close. The only way to exit the process will be to send it a signal of some sort. You might want to arrange to catch the signal so you can exit in a controlled way. Another approach might be to have the process listen on a port for a shutdown message.
It's possible. You just need a way to close the GUI programmatically right? This question might be helpful: How to programmatically close a JFrame
So the idea is when you click the button, the handler of the button will send exist signal to the GUI, and start the crawling process (probably by creating some object).
At the very least, you can always start a child console Java process (passing a generated configuration/data file if the amount of data is significant).
i want to write a clock program, that should run at background and broadcast the current time according to the system if the keys "1" and "2" are pressed together. i already have a program itself (including audiofiles and appendings) so everything i need, is to find the way to make the program window inactive, but to do it in such way that it will activate when the keys are pressed. what can i do?
On Linux with KDE you can use khotkeys to set up a keyboard hotkey which will send e.g. a dbus message to your program to tell it to reactivate. I don't know if 1 and 2 together is an allowable hotkey - it doesn't make much sense because it will likely cause a 1 or 2 to be inputted into the program you are currently using, which may or may not do anything but it's better to use a key like ctrl, alt or the windows key to avoid that problem.
In other environments / operating systems there may be something similar to khotkeys, I don't know.
I dont think Java can help you here - you're looking at something like a TSR, which unfortunately is not a Java thing. They went the way of the dinosaurs anyway, along with MSDOS.
You've to go native for something like this on the modern operating systems.
Is it possible in Delphi to keep a window on top of all other windows? For example when you have an error message in your application, let's say you want to keep the window on top and make sure the user has to click something before he can do anything else. And I really mean anything, like clicking another program in the background. And how about in Java?
No. Raymond Chen of Microsoft has a great article about why not here. The gist of it is that no matter how hard you try to keep your window on top, someone else can always come along and do the same thing.
You could do this in the old days. It was called a system modal dialog and you used the now obsolete SetSysModalWindow() function.
They were utterly repugnant and so sense and order was restored when Windows NT based versions of Windows took over.
In Delphi you can do FormStyle:=fsStayOnTop;. This will put you in front of all normal windows.
But if there are other windows which have that style set too(such as the task-bar) those might be on in front of yours. In particular among those windows the one that has focus is has the highest priority.
You can try to ensure that your window always has focus, but that's rarely a good idea. And you will incur the wraith of Raymond.
You can also use a layered window. I think those are in front of normal always-on-top windows. But this has severe side effects and is most likely not the correct choice for you.
Whenever i want to connect to internet,i double click a connection icon(i created it earlier where username and password(for broadband) are stored) and click connect.The icon is in the network places(Windows XP)
May i know how to launch this connection from java or any other language? (I am asking this because my Internet Service Provider doesn't charge anything between 2 AM and 8AM :-) )
Creating a system task to run the program. You shouldn't need Java to execute a program on windows.
To use Scheduled Tasks in XP: http://support.microsoft.com/kb/308569
This is going to be unnecessarily difficult with Java, I believe. You'd have to write some native code to do the job for you, at which point you may as well write your whole program in C# or C++ anyway.
But, since you asked for a Java approach, you might want to look at the Robot class. It lets you move the mouse to a specific location on the screen, click, and otherwise automate the manual actions that you are doing. It's a very fragile solution.
Alternatively, if you can figure out what command the network connection shortcut is invoking, you can directly invoke it from Java using Runtime.exec.
(I don't really see why Java is good for this task, though.)