I tried a addWindowListener and implement the windowClosing, it works, when I press the close button, but when I use Cmd+Q to close, the windowClosing is not being called, how can I solve it? Do I need to detect Cmd+Q on mac, Alt + F4 on windows via key listener? Is that a general listener for closing window, whatever via the close button or keyboard, or event Ctrl+Alt+Delete or Cmd+Option+Esc to focus kill? Thanks.
I'm not sure what the situation is on Macs, but on Windows you get the windowClosing() callback from the close button; Alt-F4; and if you close the app via task manager. You don't get the callback if you use task manager to kill the process, but I wouldn't expect that anyway.
You have remembered to call setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); on your JFrame instance, haven't you?
there is one more method windowClosed()
try overriding thing method. hope it will work for you.
You can use this osx library:
com.apple.eawt.ApplicationListener
handleQuit(ApplicationEvent event)
Will probably do the trick.
Information from the docs:
Called when the application is sent the Quit event. This event is generated when the user selects Quit from the application menu, when the user types Command-Q, or when the user control clicks on your application icon in the Dock and chooses Quit. You can either accept or reject the request to quit.
Of course this solution will not work on Windows. As far as I know there is however no universal solution, so this is probably the best way to go.
Sounds like you need to add some KeyListeners and a factory to detect the one you want for a particular operating system.
Check Out
As you said windowClosing is called when you click the (x) button. I am also on a mac and the way I get the CMD+Q to send a signal to the application is using Runtime.addShutDownHook
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
// code to run when CMD+Q is pressed
}
}
Related
Using reflection, I've managed to invoke the main method of another java application that uses swing to create windows. I've also been able to grab those windows and manipulate them. When I get to a certain point, I loop through the actionListeners of a certain JMenuItem and I call their actionPerformed telling them the button has been pressed. This works exactly as intended, and opens a new window. When this window opens, however, I want to do something similar to it by first getting the window and then the components inside of it.
However, as soon as the event is fired, the window is created and my program is put in a busy loop waiting for the window I want to interact with to close. This is caused by the application I am invoking, and I have no control over this, nor do I have an opportunity to do anything about it.
Here's how I am doing that event firing
for (ActionListener a : nc.getActionListeners()) {
a.actionPerformed(new ActionEvent(nc, ActionEvent.ACTION_PERFORMED,null) {});
}
What I'm thinking is I might want to have another thread that's looking for the window, but I'm not even sure if that will work..
Window has static method
public static Window[] getWindows() {
return getWindows(AppContext.getAppContext());
}
Frame has similar one
public static Frame[] getFrames()
So you can get copy of the created windows (frames) before your click emulation and compare with the new list after click to find the newly created one.
I am creating a LOGIN form that will automatically show up whenever the computer starts up.
I got it right though. My problem is, I have to disable the ALT+TAB and CTRL+ALT+DEL keystrokes when my program is already running. But the thing is, whenever I test my program, my computer gets lag. Especially when I include the method of disabling ALT+TAB.
Somebody HELP!
public void altTab ()
{
try{
Robot robot = new Robot();
while(true){
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_TAB);
this.requestFocus(true);
}
}catch(Exception e){
}
You get lag because of having an endless loop running again and again. Try putting an event on Key down and cancel it?(or release it)
You cant disable Alt+Ctrl+Del nor can you call it via your Robot class (using keypress or keyrelease - you can try it). And I don't think the code inside your while loop will run always- you are just releasing the key pressed. I am not sure about this, but you might try event handlers...
Inside that loop, you should call Thread.sleep(). Take a look at this answer, which shares much of the same code you used:
https://stackoverflow.com/a/6128105/2579661
I am working on a program, which allows the user to lock the computer, so no one else can use it. Is there anyway, I can disable the mouse, and specific keys on the keyboard? Thanks.
No but you can create have your program locking the screen and then using a MouseMovementListener so each time the mouse moves, you return it programatically to some point in the screen ( it would look like it doesn't move anymore )
over Linux you can use xinput enable id
you can get the id throw xinput without parameters.
Process p;
p = Runtime.getRuntime().exec("xinput disable 12");
I don't know of a portable way and I'm pretty sure Java does not supply anything like that in fact. However on Windows this can be done with BlockInput. But if you were going to code JNI/JNA you might as well use LockWorkStation.
I think you could do it if you are implementing the MouseListener interface. In the MouseClicked method you could check a boolean before actually performing any action. When you want to disable or enable the action change the state of that boolean.
If you want to lock the screen then you can use java Robot class. And if you want to block the keyboard and mouse events when Windows is locked then there is no need. Since locking the screen results in disabling all the inputs.
Hi all:
I have a Java Swing App. There is a button allow user to create open up a new window of the application. I use System.Exit(0) when user decides to close the application, however when I press "Close" button, both Application windows closed.
public static void main(String[] args)
{
ghMain = new GreenHouseMain();
}
Above is how I initialize the first application, then use the same code to create new GreenHouseMain Object to open second application window.
So my question is how do I close only one application window which the close button I pressed from?
Thanks all
call dispose() instead of System.exit() on the Window object that you want to close. When there are no more visible windows, the Event Dispatch thread will exit.
I assume that both windows are JFrames. If so, it is better to have the second window be a JDialog, modal or non-modal depending on your requirements. If you need both windows open and want to be able to let the user select which to close, then perhaps both need to be dialogs, though I'm not 100% sure based on the information you've presented. If these suggestions don't solve your problem, then please provide us with more details on your exact requirements.
read the javadocs for setDefaultCloseOperation. System.exit() is doing exactly what it's supposed to, so get rid of it.
I want the active window (i.e JFrame ot JDialog) to receive specific keyEvent so that whereever the focusing this keyevent is handling by the window such as ALT+F4 if you press
it wherever the focusing the active window will close,I try to override postprocesskeyEvent but it doesn't work fine
You can add a global event listener to you application using the addAWTEventListener() method in java.awt.Toolkit.
http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener%28java.awt.event.AWTEventListener,%20long%29
You will need to choose which type of events you want to receive with the event mask when you add the listener.
For example:
// Then on startup register.
AWTEventListener myGlobalKeyListener = new MyGlobalKeyListener();
Toolkey.getDefaultToolkit().addAWTEventListener(myGlobalKeyListener, AWTEvent.KEY_EVENT_MASK);
If you are trying to stop windows from shutting down your application when the user presses ALT-F4 then an event handler will not help you. I belive the operating system handles this by sending SIGTERM to the application. Java does not receive the KeyEvent for this.
The standard approach for intercepting KeyStrokes when using Swing is to use Key Bindings. Although as mentioned earlier this still won't work for Alt+F4.
If you are trying to prevent Alt+F4 from closing the window, then you need to use the setDefaltCloseOperation(...) method to do nothing. Closing an Application gives more information on this approach.