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.
Related
I don't want my input/output to appear in the integrated Terminal, I want IntelliJ to open and use a new cmd.exe window each time I execute the program. Is that possible?
Edit: For clarification, since apparently I was using wrong terminology (sorry about that): By 'integrated terminal' I meant the 'Run' window (where all the System.out.print stuff goes).
So basically I want my program's output to not be shown inside this Run window, but rather inside a new terminal window (cmd.exe preferably), just like if you would start your programs from the terminal itself.
Is that still possible? Because I think it was at one point in the past. And if not, why?
In Eclipse, I created one CN1 project and one normal Java project, which depends on the former. The latter contains some utilities (e.g., source code generation) and some JUnit tests. I use the following simple hack:
CodenameOneImplementation impl = new JavaSEPort();
Util.setImplementation(impl);
Display.init(impl);
It works, but there's a fullscreen window shown and the program doesn't terminate when main is done. I know, that it's the normal behavior for GUI applications, but I don't need any GUI as I only initialized Display, in order for Display#getResource to work.
How can I get rid of the window (or at least make it small)?
How can I terminate the program without having to call System.exit (i.e., something like running the event handling thread as daemon)?
Is there more to set up?
Use something like this:
JavaSEPort.setDefaultInitTarget(new JPanel());
This would draw the UI of the display into the blank JPanel.
About exiting the app you would need to use System.exit(0) as the EDT loop and native GUI loop are running. You can stop the EDT but that might not work well for the desktop port so just using exit is easy and common practice.
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 have a final project for computer science due tommorow, this works as an application but won't display anything as an applet which is what we need it to be. (it was coded as an app then converted to an applet)
i dont undertsand how this whole posting code things works on this site so i put it on pastebin.com
http://pastebin.com/kiFmzbfp
Is there in which I could run a jar using an applet?
Is there in which I could run a jar using an applet?
Yes. Though it is not the best way to go about showing a GUI from an applet.
Add the Jar to both the compile-time & run-time class-path (int the archive attribute) of the applet. Within the applet init(), remove start(); and add the code within the current main(String[]). Vis.
UserInterface runner = new UserInterface();
runner.Interface();
MainCode run = new MainCode();
run.Change_InterFace();
Preferably that code should be called on the EDT (Event Dispatch Thread). See Concurrency in Swing for more details.
But the code will have problems as an applet, since it attempts to access resources by File rather than URL. This has been discussed in the last 24 hours.
Noting that is 443 lines of code dump at the paste bin,this is as far as I am prepared to investigate. You have ..around 17 hours left till the dead-line, so you'd better get cracking.
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.