# Embedding Processing 3 Sketch Into Swing

**URL:** <https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410>\
**Category:** Project Guidance\
**Created:** [May 29, 2020, 5:33pm UTC](https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410 "2020-05-29T17:33:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xenthera](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@Xenthera](https://discourse.processing.org/u/Xenthera)\
**Post date:** [May 29, 2020, 5:33pm UTC](https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410/1 "2020-05-29T17:33:40Z")

</div>

Whether I need to do this or not (IE use a gui framework in processing) is not what i’m asking. I have reasons why I would like to do this, from proof of concept all the way to using processing to do visual rendering inside data driven applications i’m writing.

Anyway, i’ve done plenty of googling before resorting to bothering you all. Everything i’ve come across has not worked for one reason or another. So my question is, is this possible in processing 3? I’d be using the default renderer so no OpenGL contexts.

Thanks for any information, I really do appreciate it.

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [May 29, 2020, 9:21pm UTC](https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410/2 "2020-05-29T21:21:08Z")

</div>

Launching Processing in the default `JAVA2D` mode creates a swing frame and embeds an AWT canvas (the element that Processing draws on) into it. You can expose the frame and canvas and do anything you want with them: see [Show an desktop menu bar](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651) and [Javax swing JFrame in the main processing PApplet](https://discourse.processing.org/t/javax-swing-jframe-in-the-main-processing-papplet/16122).

Unless you _have_ to use swing, you could use Processing’s `JAVAFX` mode and create a GUI using JavaFX instead – you can even use SceneBuilder to design the UI and [load](https://discourse.processing.org/t/fxml-file-integrate-in-processing/18325) it into Processing.

---

<div class="post-metadata">

**Author:** ![Xenthera](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@Xenthera](https://discourse.processing.org/u/Xenthera)\
**Post date:** [May 30, 2020, 1:09am UTC](https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410/3 "2020-05-30T01:09:08Z")

</div>

Okay so i’ve managed to get a reference to the AWT canvas (smoothcanvas) but when I move the awt surface to my own frame’s JPanel i’m getting a BuffersNotCreated exception.

Here’s my code:

```auto
package com.company;

import processing.awt.PSurfaceAWT;
import processing.core.PApplet;
import java.awt.*;

public class Main extends PApplet {

    public static void main(String[] args) {
        App app = new App();

        PApplet.runSketch(new String[]{""}, app);

// JFrame frame = (JFrame)((processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();

        PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas)app.getSurface().getNative();

        System.out.println(canvas);

        canvas.setPreferredSize(new Dimension(400,400));

        MainForm main = new MainForm("Processing test");
        main.setVisible(true);

        main.mainPanel.add(canvas);

        

    }
}

class App extends PApplet{
    public void settings(){
        size(200, 200);
    }

    public void draw(){
        background(0);
        ellipse(mouseX, mouseY, 20, 20);
    }

    @Override
    public void keyPressed() {

    }
}

```

Here’s the error

> java.lang.IllegalStateException: Buffers have not been created  
> at sun.lwawt.LWComponentPeer.getBackBuffer(LWComponentPeer.java:519)  
> at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)  
> at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4050)  
> at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)  
> at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)  
> at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)  
> at processing.awt.PSurfaceAWT.render(PSurfaceAWT.java:298)  
> at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1548)  
> at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [May 31, 2020, 5:16pm UTC](https://discourse.processing.org/t/embedding-processing-3-sketch-into-swing/21410/4 "2020-05-31T17:16:20Z")

</div>

Not sure whether moving the surface to your own frame will work. Rather, build on the frame Processing creates.
