Loading...
Logo
Processing Forum
I'm building a small desktop application in Processing. I needed a second window to show a static image.
Now I want to close that second window from the original applet… I created a method calling exit() that causes the whole application to exit. I tried dispose() that gives me a nullpointexception… How can I close that window without closing the whole application?

I'm using this code I found on the old Processing forum:

Copy code
  1. PFrame f;
    secondApplet s;

    void setup() {
     size(320, 240);
     PFrame f = new PFrame();
    }

    void draw() {
      background(255,0,0);
       fill(255);
       rect(10,10,frameCount%100,10);
       s.background(0, 0, 255);
       s.fill(100);
       s.rect(10,20,frameCount%120,10);
       s.redraw();
    }

    public class PFrame extends Frame {
        public PFrame() {
            setBounds(100,100,400,300);
            s = new secondApplet();
            add(s);
            s.init();
            show();
        }
    }

    public class secondApplet extends PApplet {
        public void setup() {
            size(400, 300);
            noLoop();
        }

        public void draw() {
        }
    }


Replies(5)

I figured it out. The frame was not accessible by the main window…
f.dispose(); works.
Hi,
I'm really interested to this post because I also can't get the second windows closed. Can you explain to me which is the correct way to get it?? Thank you very much! really appreciated!
me too. funny thing is: i can use dispose only inside of the method that i use for constructing the window. there it does what it should do. but if i try to trigger it outside that function i constantly get error messages...

does the same here in the example code:

Copy code
  1. PFrame f;
    secondApplet s;

    void setup() {
     size(320, 240);
     PFrame f = new PFrame();
    }

    void draw() {
      background(255,0,0);
       fill(255);
       rect(10,10,frameCount%100,10);
       s.background(0, 0, 255);
       s.fill(100);
       s.rect(10,20,frameCount%120,10);
       s.redraw();
    }

    public class PFrame extends Frame {
        public PFrame() {
            setBounds(100,100,400,300);
            s = new secondApplet();
            add(s);
            s.init();
            show();
        }
    }

    public class secondApplet extends PApplet {
        public void setup() {
            size(400, 300);
            noLoop();
        }

        public void draw() {
        }

    void mousePressed()
    {
      f.dispose();
    }

--> NullPointer Exception...
Super-classical error: you redeclare f in setup(), so it is hiding the global one, which remains null.
Just remove the PFrame declaration from setup().
Hi everybody!
This is my code
Copy code
  1. import controlP5.*;
  2. import processing.serial.*;


  3. PFrame f;
  4. secondApplet s;
  5. ControlP5 cp5;
  6. Serial serial;
  7. Serial myPort;
  8. ListBox l;

  9. void setup() {
  10.   size(320, 240);
  11. }
  12. void draw() {
  13.   background(0, 255, 0);
  14.   fill(255);
  15. }
  16. public class PFrame extends Frame {
  17.   public PFrame() {
  18.     setBounds(700, 500, 100, 80);
  19.     s = new secondApplet();
  20.     add(s);
  21.     s.init();
  22.     show();
  23.   }
  24. }
  25. public class secondApplet extends PApplet {
  26.   public void setup() {
  27.     size(400, 300);
  28.   }
  29.   public void draw() {
  30.       background(255, 0, 0);

  31.   }
  32. }
  33. void keyPressed() {
  34.   if (key==' ') {
  35.     key=0;
  36.     PFrame f = new PFrame();
  37.     s.background(0, 0, 255);
  38.     s.fill(100);
  39.     s.rect(10, 20, frameCount, 0, 10);
  40.     s.redraw();
  41.   }
  42. }

  43. void mousePressed()
  44. {
  45.   f.dispose();
  46. }
But the solution You suggested does not work for me. In particular if I have a click inside the first window this causes a crash. If I don't click inside the first window but call for the second one and try to close it, it still not works.
Can you help me??