4

I am currently working on a research project for a University in which I am doing GUI interactions with my database and launching an external program based on the data. I'm using runtime commands (once the OS is detected) to launch that external program with the selected data.

My question is how can I embed an external program's GUI inside a Java frame, if that is even remotely possible?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Mjones8323
  • 99
  • 1
  • 8
  • Is it a GUI, or do you want the console output displayed in your JFrame? – Lawrence Dol Jun 29 '11 at 17:49
  • I want the program window displayed in my JFrame in my GUI project. The program I want to load onto the JFrame is a visual molecular program, not a console program. – Mjones8323 Jun 29 '11 at 18:00
  • So yes, the program I want to run in a JFrame is a GUI. – Mjones8323 Jun 29 '11 at 18:13
  • Not really feasible. Maybe there is some crossplatform library with implementations for Windows and Linux to fetch Window handles, place and resize them. But how to support Windows 12 and such. – Joop Eggen Oct 15 '21 at 09:15
  • A more practical solution would be to ensure that the external program has the input it needs to run independently from your Swing GUI and run the two GUIs in parallel, either on the same monitor or on separate monitors. – Gilbert Le Blanc Oct 15 '21 at 09:20

4 Answers4

2

Given the clarifying comments on the question, the short answer is "no, you can't do that".

Java cannot display a native program's GUI within a JFrame, even if the target program was actually architected to allow it's GUI to be presented within another program's frame.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
  • There might be system-dependent ways of doing this, though. – Paŭlo Ebermann Jun 29 '11 at 18:40
  • @Paulo: I have used JNI quite a bit, and even with that I can't see a direction that does what @Cecil wants. – Lawrence Dol Jun 29 '11 at 21:30
  • Do you know if C++ can do what I want? – Mjones8323 Jun 30 '11 at 18:34
  • I do not know how it is done, but Eclipse can do this perfectly. For example, if you have a csv or other excel file, you can open it with the `(Right-Click)->Open With->In-Place Editor` and the excel window will be opened inside of the eclipse editor. – TankWhoCriedTom Sep 15 '14 at 00:54
  • @TheHarrisonCrafter: Eclipse uses SWT not Swing. It is true that Windows APIs allow embedding of other applications, so, contrary to my other comment, it must be possible via JNI; but not directly using just Swing. – Lawrence Dol Sep 15 '14 at 17:42
0

Are you using a console application? You have to intercept its stdout to do it correctly. So you can show the text that the 3rd party application is outputting in an UI control that you can put into JFrame.

Olaf
  • 6,249
  • 1
  • 19
  • 37
  • No, I'm using my custom code combined with Netbeans GUI code. Would the process for that be similar? – Mjones8323 Jun 29 '11 at 17:57
  • The application I want to run in the JFrame is a GUI – Mjones8323 Jun 29 '11 at 18:14
  • @Cecil O'Dell: Are you trying to show GUI of one application inside GUI of another application? I believe that would be prohibitively complicated. If you wrote that application appropriately separating layers (UI, business logic, data access), you can try to build some API on top of it so that GUI of the "master" application can substitute the original GUI. You can also turn it into a web service or a web application but it all seems to be an overkill. – Olaf Jun 29 '11 at 18:17
  • Would it be that difficult to accomplish? It was my initial thinking that there should be some class to handle most of the complications for you. – Mjones8323 Jun 29 '11 at 18:22
  • @Cecil O'Dell: What is easy for the web-based applications, is sometimes prohibitively complicated for the desktop applications. Unless you were really diligent about your applications' design, converting them to web architecture would be a significant effort. If you developed both applications, can you recombine them in one desktop application? – Olaf Jun 29 '11 at 18:30
0

It depends from the application you want to embed to the JFrame, but you can try to use jawt: https://docs.oracle.com/javase/9/docs/specs/AWT_Native_Interface.html

You will be able to get the native OS specific window handle and can draw on top of it, or can you use it as a container. Note that only HW components are supported, so you will need to add Panel/Canvas to the JFrame and then use that for your native app.

This is similar to this question: Native JNI/JAWT Swing application runs successfully on Java 6, but fails on Java 7 (64-bit Windows 7 OS)

serb
  • 21
  • 2
-1

Use java.lang.Process or java.lang.Runtime.exec.

http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • As I stated, I'm already using runtime in the project, and my external program gets launched successfully. The problem is, I now want that program to appear in the jFrame, not outside of it. – Mjones8323 Jun 29 '11 at 17:40