We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › MessageBox / MsgBox
Pages: 1 2 
MessageBox / MsgBox (Read 9152 times)
MessageBox / MsgBox
Jan 2nd, 2010, 6:18am
 
Hello,

I am looking for a simple MessageBox / MsgBox but I can't find the command. I didn't find it in the libraries.

Please help!

Happy New Year!

Chrisir

Re: MessageBox / MsgBox
Reply #1 - Jan 2nd, 2010, 7:37am
 
javax.swing.JOptionPane.showMessageDialog(null, "Hello World");
Re: MessageBox / MsgBox
Reply #2 - Jan 2nd, 2010, 9:41am
 
Thank you so much!

You really made my day!

That should be referred to in the reference!!!

Thank you!

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html


void MsgBox( String Msg, String Titel ){

 javax.swing.JOptionPane.showMessageDialog ( null, Msg, Titel, javax.swing.JOptionPane.INFORMATION_MESSAGE  );
 
}

HELP
Reply #3 - Jan 7th, 2010, 8:24am
 
Hello,

please help me with this one.

I am using

Quote:
void MsgBox( String Msg, String Titel ){
  // Messages
  javax.swing.JOptionPane.showMessageDialog ( null, Msg, Titel, javax.swing.JOptionPane.INFORMATION_MESSAGE  );

}



but I run into deep troubles.

He can't restore the screen or program hangs.

I tried to give it more memory but to no avail.

Can anybody tell me where the problem is?

Thank you!

Greetings, Chris

Re: MessageBox / MsgBox
Reply #4 - Jan 7th, 2010, 8:37am
 
Try to wrap the suspect portion of the code in a try / catch statement.
Code:
try {
 //run a bit of code
} catch (Exception e) {
  javax.swing.JOptionPane.showMessageDialog(null, "I'm over here with this error: " + e);
}


Also note that the MessageDialog will not break through a full screen program in presentation mode.  It takes exclusive access of the screen and will not let the dialog box through... so even if you had one, you wouldn't know it.  Use the fullscreen library in softmode in that case or test it in non-fullscreen.
Re: MessageBox / MsgBox
Reply #5 - Jan 7th, 2010, 12:18pm
 
Chrisir wrote on Jan 2nd, 2010, 9:41am:
That should be referred to in the reference!!!

Unlikely, Processing authors warn against the use of Swing in sketches... and you saw the hard way why!

Now, the [Pretty much solved] Swing in P5 thread suggest a smart solution to your problem.
Re: MessageBox / MsgBox
Reply #6 - Jan 8th, 2010, 2:31pm
 
That's way out...

I would really appreciate it if
ControlP5 by andreas schlegel
could provide a simple MsgBox...

Since I am destroying my code with implementing my own ugly MsgBox...    Cry

Thanks!

Greetings, Chris

Re: MessageBox / MsgBox
Reply #7 - Jan 8th, 2010, 11:17pm
 
Use noLoop() before you run the msgbox and then use Loop() right after your msgbox. I have done this without any problem. I think the problem has something to do with how mouse event is handled and I was in deep hole before I found this way works. I know someone is going to say No to awt or swing but controlP5 really doesn't do the job in some special cases.
This dialog is going to block execution until it is dismissed by ok or cancel button. If that is not what you want, you can always use annoymous inner class thingy to start a separate thread to run your dialog and the main program will not wait for the dialog.
Re: MessageBox / MsgBox
Reply #8 - Jan 9th, 2010, 6:09am
 


Thanks to all of you for helping me!

Chrisir

Re: MessageBox / MsgBox
Reply #9 - Jan 10th, 2010, 1:39pm
 
hi,
you could create your own message box with available controllers in controlP5. maybe the code below helps for now, but i should add this to controlP5 anyway.

best,
andreas
Quote:
import controlP5.*;

ControlP5 controlP5;
int messageBoxResult = -1;
ControlGroup messageBox;

void setup() {
  size(640,480);
  frameRate(30);
  controlP5 = new ControlP5(this);
  createMessageBox();
}

void draw() {
  if(messageBox.isVisible()) {
    background(50);
  } else {
    if(messageBoxResult==1) {
      background(0,255,0);
    } else {
      background(255,0,0);
    }
  }
}


void createMessageBox() {
  // create and set a ControlFont, in case the
  // the default controlP5 font is too small for you / the user
  ControlFont font = new ControlFont(createFont("Arial",12),12);
  // if the size of controlP5's default pixel font is not
  // too small, disable the setControlFont command below.
  controlP5.setControlFont(font);
  
  // create a group to store the messageBox elements
  messageBox = controlP5.addGroup("messageBox",width/2 - 150,100,300);
  messageBox.setBackgroundHeight(120);
  messageBox.setBackgroundColor(color(0,128));
  messageBox.hideBar();
  
  // add a TextLabel to the messageBox.
  Textlabel l = controlP5.addTextlabel("messageBoxLabel","Some MessageBox text goes here.",20,20);
  l.moveTo(messageBox);

  // add the OK button to the messageBox.
  // the name of the button corresponds to function buttonOK
  // below and will be triggered when pressing the button.
  controlP5.Button b1 = controlP5.addButton("buttonOK",0,65,80,80,24);
  b1.moveTo(messageBox);
  // by default setValue would trigger function buttonOK,
  // therefore we disable the broadcasting before setting
  // the value and enable broadcasting again afterwards.
  // same applies to the cancel button below.
  b1.setBroadcast(false);
  b1.setValue(1);
  b1.setBroadcast(true);
  b1.setCaptionLabel("OK");
  // centering of a label needs to be done manually
  // with marginTop and marginLeft
  b1.captionLabel().style().marginTop = -2;
  b1.captionLabel().style().marginLeft = 26;
  
  // add the Cancel button to the messageBox.
  // the name of the button corresponds to function buttonCancel
  // below and will be triggered when pressing the button.
  controlP5.Button b2 = controlP5.addButton("buttonCancel",0,155,80,80,24);
  b2.moveTo(messageBox);
  b2.setBroadcast(false);
  b2.setValue(0);
  b2.setBroadcast(true);
  b2.setCaptionLabel("Cancel");
  b2.captionLabel().toUpperCase(false);
  b2.captionLabel().style().marginTop = -2;
  b2.captionLabel().style().marginLeft = 16;
}

// function buttonOK will be triggered when pressing
// the OK button of the messageBox.
void buttonOK(int theValue) {
  println("a button event from button OK.");
  messageBoxResult = theValue;
  messageBox.hide();
}
// function buttonCancel will be triggered when pressing
// the Cancel button of the messageBox.
void buttonCancel(int theValue) {
  println("a button event from button Cancel.");
  messageBoxResult = theValue;
  messageBox.hide();
}

void keyPressed() {
  // press space in case you need to show the messagebox again.
  if(key==' ') {
    messageBox.show();
  }
}

Re: MessageBox / MsgBox
Reply #10 - Jan 10th, 2010, 2:13pm
 
I also need mouse actions on the drawing canvas so I can't just hand over all mouse event processing to controlP5.
Re: MessageBox / MsgBox
Reply #11 - Jan 10th, 2010, 3:57pm
 
hi liudr, if you add the following code snippet after background(50); (when the messageBox is active and visible) to the sketch i posted earlier. you will see that you still have access to all mouse events on the drawing canvas. the messageBox is just drawn on top of the canvas, but does not disable any (processing) mouse events.

Code:

 if(mousePressed) {
   fill(255);
 } else {
   fill(128);
 }
 rect(mouseX, mouseY, 100,100);

Re: MessageBox / MsgBox
Reply #12 - Jan 10th, 2010, 4:12pm
 
Thanks. I guess I expressed what I don't want instead of what I want. Say I have a program that will draw a dot at the position I click. Now I pop up a controlP5 element and click on a radio button, the canvas also draws a point because both my program in mouseClicked() and controlP5 get the click. How do I make sure the click is only passed on the controlP5 and not further on to the canvas?
Re: MessageBox / MsgBox
Reply #13 - Jan 10th, 2010, 4:58pm
 
ah ok. use

Code:

if(controlP5.window(this).isMouseOver()==true) {
 // isMouseOver() returns true if the mouse is on top of a controller
 // if this is the case, do not draw on to the canvas.
} else {
 // draw on to the canvas.
}

Re: MessageBox / MsgBox
Reply #14 - Jan 10th, 2010, 6:42pm
 
Thanks sojamo. I'll give that a try.
Pages: 1 2