I am writing a program that will be saved as a .JAR and run from the console. I want the program to save to a file whenever a user closes its window, but I don't know if you can detect the window being closed. I know I could use a WindowListener in JFrame to detect the Close Event, run the save function, then end the program, but with what the program is and how I set it up I don't really want to do that. I am running on Windows 8, if that makes a difference.
Related
I am not even sure if it is possible for a java program to register mouse clicks outside of a java component.
What I want to do is run a program (specifically java) that will continuously run and count how many times a user right-clicks until I manually end the process.
Is this possible in java? Is this possible in another language?
Suppose I have a set of codes to display a JFrame, a JPanel, and a JLabel. This works fine if I run it as a script file. It just shows a tiny window with a label that says "A label" exactly like you would expect:
frame = javax.swing.JFrame('Test');
panel = javax.swing.JPanel();
label = javax.swing.JLabel('A label');
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
The problem comes when I compile this as an exe file with the deploytool. It will compile and I can run the program, but the frame will show up for about 3 seconds or so then disappear. If I run from inside Matlab with !main.exe, there is no error message when the window disappears (I don't want to say it crashes because there is no error message). Neither is there one if I run the executable from the Windows command prompt (same results -- shows for a few seconds and then crashes).
Any ideas what is going on here? I can compile other files just fine. Is the problem because I included the javax.swing elements?
Many thanks for your help.
UPDATE
This feels like a really cheap hack, but having a while loop that pauses Matlab as long as the JFrame is open does the trick. So now the question is, is there a better way to do this?
The problem is probably that your main M-code function finishes executing, and since there are no figures up, Matlab decides to exit. In a Java Swing program, what would happen is that things would keep going until all the Swing windows are closed or you explicitly terminate the program. Since this is a Matlab program, the layer that's "in control" is the Matlab handle graphics layer, so you need to either have the main function executing or a figure up. (In interactive Matlab, it'll keep running as long as you have the IDE up, but there's no IDE in a compiled Matlab program, so when its work is done, it exits.)
The "Right Thing" to do from MathWorks' perspective is to probably buy the Matlab Builder JA toolbox, build the Matlab part of your program in to a Java library, include that in a main program which you write in Java. That way the Java layer is "in control" of the main execution sequence, and the "stay running as long as there are Java windows open" logic you're expecting will be in effect.
If you want to hack this to make it work in your current program structure, your invisible figure window might be a good one. Though you will need to make it Visible to have it work; invisible figures don't count for keeping a Matlab GUI running. You may be able to hide it from the user by changing its position to move it entirely off the user's screen.
Then you need to terminate the program somehow. Some part of your code is going to know when the program should end. That sounds like it's the Java part of your code. From there, you could just call java.lang.System.exit(). If you need to do Matlab-layer stuff, you could exit from M-code by communicate the "it's time to exit" back to your Matlab code, which could then call exit() or close that figure. You could do this by setting a public class variable in one of your Java classes, and have a Matlab timer object that checked that variable every 500 milliseconds or so.
If the condition that ends the program is that all your Java Swing windows get closed, that's a little tougher. Because the Matlab figure window itself is a Java AWT or Swing window, so as long as that's open you're not getting down to zero windows. What you could do is have that Matlab timer, instead of looking for a class variable, check the list of open Java windows and see if the Matlab figure is the only one left, and if so, close it or exit explicitly.
I am creating an IDE using swing which can compile and run user's program. if user write swing program and run the program which have frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); inside that program, it successfully run. but when user close there program, my IDE also get closed. How to get rid from this. I want that my IDE don't get closed.
Is there any procedure that my swing application still get running when user close there program?
For Example: in Netbeans IDE, when we run a swing program and close that program, Netbeans IDE does not get closed.
You could change their close operation to something else such as DISPOSE_ON_CLOSE or DO_NOTHING_ON_CLOSE.
i.e.,
OtherGUI otherGui = new OtherGui();
otherGui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
otherGui.setVisible(true);
As an aside, you might want to tell us more about your current design of running other programs, as that makes me worry about possible issues. What exactly are you doing?
Edit
You state:
actually i am running that program via reflection and i don't have right to change user program, so what should i do in such case?
and
I am creating an IDE using swing which can compile and run user's program. if user write swing program and run the program which have frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); inside that program, it successfully run. but when user close there program, my IDE also get closed. How to get rid from this. I want that my IDE don't get closed.
I've no experience with this, but I suspect that you will want to create each launched GUI in its own JVM, which you'd do not with reflection (I don't think), but by creating a new process with a ProcessBuilder, taking care to handle all streams appropriately, and calling Java directly in your process, launching the program.
I created a JFrame with a few labels. I need to display a JOptionPane with the message "welcome user" once. If the frame is re-opened before rebooting Windows, the JOptionPane should not appear, but if Windows is rebooted, the JOptionPane should appear again.
How am I able to detect whether the system has been rebooted, since the last time my application ran?
So, as #Kayaman mentioned, there isn't an isRebooted() method in Java... but you can make one.
Assuming you are working on Windows platforms (you would need to implement a separate version for other OSes), you could query the system event log. With all the stuff that gets put in there I reckon it would include an event for "logged in", so all you need to do is find a way to look up the last "logged in" time and see if it has changed since the last time you checked.
Accessing the event log is a windows specific trick, and so won't be in the native java api, however there is a question on how to access the event log from Java which you could use as a base.
Drop a BAT script in the Autostart folder of the user which creates a file in a specific location:
echo JUST_STARTED > C:\NAME_OF_YOUR_APP.TXT
In your app, check whether the file exists. If it does, delete it and display the message.
The goal is to have the user select a java program, then my program opens up a JInternalFrame with a JEditorPane inside it as the console and places said JInternalFrame in a JDeskopPane. Is it possible to change all the Windows the user's program may open into JInternalFrames and place them in said JDesktopPane, as well?
(individual question from IDE-Style program running)
I'm quite sure that this would not be possible to do without tampering with the binaries of the program that you're launching. If the target program performs something like new Window().show(), you'll have little possibilities to "hook into" the system, and tell it to swap it for a JInternalFrame.
What I'm saying is that if the program is written and compiled to show a top-level window, there is little you could do to change that. There is no "hook" into the system, with which you can say "put all future Windows into this JInternalFrame.