Loading...
Logo
Processing Forum
I want a confirm dialog, but it's important that draw keeps running.
I had a confirm dialog working but it paused draw, now i placed it in a separate thread but i don't see the dialog anymore.

Also i'm not sure about:

    closeFrame = closePane.getFrameForComponent(p);

In draw i used 'this', but if i used 'this' in the CloseThread (bad name btw) class then i got a error.
Getting the Frame is important for me since i need to get the position.

Copy code
  1. import javax.swing.JOptionPane;
  2. import java.awt.Point;




  3. CloseThread closeThread;

  4. void setup() {
  5. }

  6. void draw() {
  7.   background(0);
  8.   text(frameCount, 20, 20);
  9. }

  10. void keyPressed() {
  11.   if (key == ESC) {
  12.     key = 0;
  13.     closeThread = new CloseThread(this);
  14.     println("close");
  15.   }
  16. }


  17. class CloseThread extends Thread {
  18.   
  19.   PApplet p;

  20.   JOptionPane closePane;
  21.   Frame closeFrame;
  22.   int response;

  23.   CloseThread(PApplet p) {
  24.   }

  25.   void start () {
  26.     super.start();

  27.     if (response != 1) {
  28.       response = closePane.showConfirmDialog(null, "Are you sure you want to quit?");
  29.     }
  30.     closeFrame = closePane.getFrameForComponent(p);
  31.     Point closeLoc = closeFrame.getLocation();

  32.     println(response);
  33.     println(closeLoc.x);
  34.   }

  35.   void run () {
  36.   }

  37.   void quit() {
  38.     interrupt();
  39.   }
  40. }

Replies(5)

import javax.swing.JOptionPane;
import java.awt.Point;

CloseThread closeThread;

void setup() {
}

void draw() {
  background(0);
  text(frameCount, 20, 20);
}

void keyPressed() {
  if (key == ESC) {
    key = 0;
    closeThread = new CloseThread(this);
    closeThread.start();
  }
}

class CloseThread extends Thread {
  
  PApplet p;

  Frame closeFrame;
  int response;

  CloseThread(PApplet p) {
  }

  void run () {
    response = new JOptionPane()
      .showConfirmDialog(null, "Are you sure you want to quit?");
    Point closeLoc = frame.getLocation();
    // java.awt.Frame frame - is a field of PApplet

    println(response);
    println(closeLoc.x);
    
    if(response == 0) exit();
  }
}


thanks that's halfway. I'm only interested in the location of the confirm dialog and not the PApplet.
I tried your code with some changes but it reports always zero.

Also how did you paste the code here with keeping the colors like that?

Copy code
  1. import javax.swing.JOptionPane;
  2. import java.awt.Point;

  3. CloseConfirm closeConfirm;
  4. //MoveCloseConfirm moveCloseConfirm;

  5. void setup() {
  6. }

  7. void draw() {
  8.   background(0);
  9.   text(frameCount, 20, 20);
  10.   if (closeConfirm != null) {
  11.     if (closeConfirm.confirm != null) {
  12.       Point  closeLoc = closeConfirm.confirm.getLocation();
  13.       println(closeLoc.x);
  14.     }
  15.   }
  16. }

  17. void keyPressed() {
  18.   if (key == ESC) {
  19.     key = 0;
  20.     closeConfirm = new CloseConfirm(this);
  21.     closeConfirm.start();
  22.     // moveCloseConfirm = new MoveCloseConfirm(closeConfirm);
  23.     // moveCloseConfirm.start();
  24.   }
  25. }

  26. // - - - - - - - - - - - - - - - - - - - - - - -

  27. class CloseConfirm extends Thread {

  28.   PApplet p;

  29.   JOptionPane confirm;
  30.   Frame closeFrame;
  31.   int response;

  32.   CloseConfirm(PApplet p) {
  33.   }

  34.   void run () {
  35.     confirm =  new JOptionPane();
  36.     response = confirm.showConfirmDialog(null, "Are you sure you want to quit?");
  37.     //Point closeLoc = frame.getLocation();
  38.     // java.awt.Frame frame - is a field of PApplet

  39.     //println(response);
  40.     //println(closeLoc.x);
  41.     // println(closeLoc.y);
  42.     Point closeLoc = confirm.getLocation();
  43.     println("x: "+closeLoc.x);


  44.     if (response == 0) exit();
  45.   }
  46. }

  47. // - - - - - - - - - - - - - - - - - - - - - - -
  48. /*

  49. class MoveCloseConfirm extends Thread {

  50.   CloseConfirm closeConfirm;

  51.   MoveCloseConfirm(CloseConfirm closeConfirm) {
  52.     this.closeConfirm = closeConfirm;
  53.   }

  54.   void run() {
  55.     println("works");
  56.   }
  57. }

  58. */
This was a huge pain to figure out. I dislike trying to work with the UI toolkit stuff.

import javax.swing.JOptionPane;
import javax.swing.JDialog;

CloseThread closeThread;

void setup() {
}

void draw() {
  background(0);
  text(frameCount, 20, 20);
}

void keyPressed() {
  if (key == ESC) {
    key = 0;
    closeThread = new CloseThread(this);
  }
}

class CloseThread extends Thread {
  
  PApplet p;
  JOptionPane jop;

  CloseThread(PApplet p) {
    jop = 
      new JOptionPane(
        new String("Qestion?"), 
        JOptionPane.QUESTION_MESSAGE,
        JOptionPane.YES_NO_OPTION
      );
    start();
  }

  void run () {
    JDialog jd = 
      jop.createDialog(frame, "Title");
    jd.setVisible(true);
    println("re " + jop.getValue());
    println("loc " + jd.getLocation().x);
  }
  
}
 
Copy code with syntax highlighting: edit -> copy as HTML. Then in the forum, click the down triangle thing next to the smiley and use insert HTML and paste.
Thanks a lot.
I work on something silly and thanks to you it get's better :D
..hallo,
how can I get the answer out of the class CloseThread?

..because I use this class for multiple confirmation dialog box.....to do something else depending on Gevent that call the  closeTherad object.

Is it possible to do this?