# Processing in Swing

**URL:** <https://discourse.processing.org/t/processing-in-swing/22120>\
**Category:** Coding Questions\
**Created:** [June 23, 2020, 11:32pm UTC](https://discourse.processing.org/t/processing-in-swing/22120 "2020-06-23T23:32:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![creepercrack](https://avatars.discourse-cdn.com/v4/letter/c/3bc359/32.png) [@creepercrack](https://discourse.processing.org/u/creepercrack)\
**Post date:** [June 23, 2020, 11:32pm UTC](https://discourse.processing.org/t/processing-in-swing/22120/1 "2020-06-23T23:32:50Z")

</div>

Is there any way to get Processing to render into a JFrame (or something along those lines)?  
I’ve been reading and trying for myself but coudn’t get it to work  
The idea is to be able to make a UI using swing on top of the image rendered by processing

Like in the image but instead of with my own ui, with swing

 ![Sin título](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b9fcf8d9b06bdc37736354a7fce6c31bb95aef56.png)

I don’t want to use G4P or other libraries, as I said, the idea is to draw swing UI on top of the game

If that can’t be done for whetehver reason, it would be cool to know a workaround or similar

I can also make side windows and make the UI there but that would be suboptimal

Thank you!

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [June 24, 2020, 6:23am UTC](https://discourse.processing.org/t/processing-in-swing/22120/2 "2020-06-24T06:23:19Z")

</div>

I am unsure about any actual proper ui things for processing, this is what I have always done:

in draw:

```auto
mouse();
if (button(20,20, 100, 20)) {
    // do something
}

```

A class for drawing and detecting the button presses

```auto
boolean button(int x, int y, int w, int h) {
    if (mouseX> x && mouseX< x + w && mouseY > y && mouseY<y+h) { // if mouse is inbounds
         fill(0); // draw button in hovered state
         rect (x,y,w,h);
         if (click) {
              return true;
         }
    } else {
         fill(255); // draw button in non hovered state
         rect (x,y,w,h);
    }
    return false;
}

```

A method for getting a single frame of true for each click

```auto
boolean click, pclick;
void mouse(){
  if (mousePressed && pclick){
    pclick=false;
    click=true;
  } else if (!mousePressed) {
    pclick = true;
    click=false;
  } else {
    click=false;
  }
}

```

From here you can customize how the button is drawn from button(), and add a string variable for the button label.

But again, I dont know if there is any proper UI stuff, I just prefer this for its customization possibility’s.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 24, 2020, 6:57am UTC](https://discourse.processing.org/t/processing-in-swing/22120/3 "2020-06-24T06:57:24Z")

</div>

If you are using the standard JAVA2D mode then the sketch is already running in a JFrame and you can get a reference to it with

```auto
JFrame frame;

frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();

```

With the frame reference you should be able to add Swing controls. to the sketch but personally I would not recommend it because there is likely to be conflicts between Processing’s event handling thread and Swings event handler.

A better approach is probably to use the PGraphics class inside a Swing application and avoid using the PApplet class which would get rid of Processing’s event thread altogether.

---

<div class="post-metadata">

**Author:** ![creepercrack](https://avatars.discourse-cdn.com/v4/letter/c/3bc359/32.png) [@creepercrack](https://discourse.processing.org/u/creepercrack)\
**Post date:** [July 2, 2020, 9:26pm UTC](https://discourse.processing.org/t/processing-in-swing/22120/4 "2020-07-02T21:26:30Z")

</div>

Sorry for the slow response but I cannot get your solutions to work.  
The first one I tried and I probably did something wrong:

```auto
import javax.swing.*;
import processing.awt.PSurfaceAWT;

JFrame ventana;

void setup() {
  size(640, 360, JAVA2D);

  ventana = (JFrame) ((PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  ventana.add(new JTextField());

  // Tried this to see if it was an issue with redrawing the frame but no luck
  ventana.revalidate();
  ventana.repaint();
}

```

I haven’t tried the second solution due to my limited knowledge of Processing working as a Java library.  
If you can, please write me a small working example of both solutions so I can study how they work.  
Thank you and have a nice day.
