embedding PApplet in Java JFrame

till processing 3 i have often made that successfully. Yesterday, answering to some question that you can see here forum.processing.org/two/discussion/12624/can-i-launch-an-instance-of-processing-inside-of-a-control-on-a-sketch#latest i posted the code you can see at the same url. This code works, i have used it many times and tested the snippet i put. Yet theOP said me "this code does not work, it fires that the applet.init() method does not exist" and he added that he was using processing 3. So, i went to the docs / changes && found that:

"As of Processing 3.0, we have removed Applet as the base class for PApplet. This means that we can remove lots of legacy code, however one downside is that it's no longer possible (without extra code) to embed a PApplet into another Java application"

ok. But i would be very happy to understand by an example what kind of "extra code" i could use... thanks in advance


  • @GoToLoop== thanks, i have not read these posts, very interesting, before; but if i resume solution is to look at runsKetch() method???

  • edited October 2015 Answer ✓

    Functions main() & runSketch() always existed in Processing. At least since v1.5.1.
    The Frame class approach was never necessary in most cases and shouldn't even be presented as 1st option for folks looking for multiple windows.

  • @GoToLoop== i knew for main(), but never saw nor used runSketch()...Now, for better understanding, i have to work with your code... i am somewhat puzzled by PS3 by settings() vs setup() and so on and finally by the exact "lifeCycle" of a processing sketch... thanks

  • @GoToLoop==

    ok, i have given a look to the code && i understand better; as for settings() i have found the infos and it's ok, it could be useful. As for the way to create your second window through this mysterious runsKetch() method (found nothing about it till now) and a class extending PApplet, i have some questions:

    --- in the runSketch() method you pass a String[] as argument, but what can you put in this one??? - I see display, i see location but what can i add??? - iHave tried with "--background() commenting your random() but it does nothing...

    --- as for a JFrame you can easily set it to undecorated or... and also set the way it interacts with user (resizable...NOTHING.ON.CLOSE...and so on). Is it possible to do the same with your projector ??? - In the code i have posted (which does not run in PS3 of course) if the user closes 1 window, the sketch continues; with your code if he does the same the twos windows are closed && the sketch stops running...

  • When using runSketch(), those arguments are totally optional.
    We can simply pass some String[] w/ only an empty "" element in it.
    Just make sure it has at least some invalid/unrecognizable argument in it though. @-)
    "" is invalid btW. ;;)

    We don't have control of what is actually created inside runSketch(). :(
    Most we can do is pass those String[] arguments. Sorry... 8-|

  • @GoToLoop== - bad news for the first question (yet i have seen that you can set the location with these args && i dont understand why you cannot set background...or more) - but for the second one??? Bad news also??? but many thanks because without your answer i probably loose 12 hours for nothing!

  • edited March 2018

    Even though we don't have control over runSketch(), default renderer JAVA2D is still based on JFrame.

    In this forum thread below there's an example that use (PSurfaceAWT) over variable surface.
    Then calls getFrame() in order to get the "real" renderer of it:

    Who knows, ((PSurfaceAWT)getSurface()).getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); might work! [-O<

  • @GoToLoop:: i'll see that!!! - But let me say that what you said before "The Frame class approach was never necessary in most cases and shouldn't even be presented as 1st option for folks looking for multiple windows." == true, it is (it was) because P5 (&&G4P lib!) could solve easily the problem. As for me, now, if i have a problem of this kind i leave processing and go to java::: more simple!

  • edited October 2015 Answer ✓

    Here's what I've come up with: :>

    // forum.Processing.org/two/discussion/12774/embedding-papplet-in-java-jframe
    // forum.Processing.org/two/discussion/12260/processing-3-init-disappearance
    // 2015-Oct-01
    import processing.awt.PSurfaceAWT;
    import javax.swing.JFrame;
    void setup() {
      JFrame jframe = getJFrame();
    JFrame getJFrame() {
      PSurfaceAWT surf = (PSurfaceAWT) getSurface();
      PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas) surf.getNative();
      return (JFrame) canvas.getFrame();

    I believe that excerpt can be used for PApplet instances created outta runSketch() too.

  • THANKS: that is answering! ( though i always was told to NOT mix AWT && Swing...)

  • edited October 2015

    According to setDefaultCloseOperation() method:

    We've got 4 constants to choose from:


    Dunno which 1 you're looking for though. You gotta check it out for yourself! :-h

  • @GoToLoop:: "I believe that excerpt can be used for PApplet instances created outta runSketch() too." i ll try tomorow and tell you ^:)^

  • ... though i always was told to NOT mix AWT && Swing

    Swing package depends on AWT package! :-B

  • @goToLoop:: i know, i have choosen DO_NOTHING_ON_CLOSE because i have add my own windowListener overriding the closeWindow:: not any problem with that!

  • @ goToLoop:: according to oracle:

    You risk getting a very different look and feel if you mix AWT and Swing UI components
    Swing components are "lightweight" (rendered by Java) while AWT components are "heavyweight" (implemented as components in the host platform) - this means you will have problems if you put AWT components inside Swing components (the other way round is fine)
  • @ goToLoop== but that is another problem!!!! :))

  • edited October 2015

    There's nothing getting mixed up! JFrame class is Swing.
    And in turn, JFrame directly extends Frame, which is AWT:

    1. http://docs.Oracle.com/javase/8/docs/api/javax/swing/JFrame.html
    2. http://docs.Oracle.com/javase/8/docs/api/java/awt/Frame.html

    JFrame: An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture.

  • @GoToLoop: right, in THIS case... and... but i am tired out!

  • @GoToLoop::

    Not Found

    The requested URL /reference/javadoc/core/processing/awt/PSurfaceAWT.html was not found on this server.


  • Hi, how to do the same with FX2D? I cannot figure out how to retrieve the Stage of the PApplet...? Thanks.

Sign In or Register to comment.