Loading...
Logo
Processing Forum

Display a message box

in Programming Questions  •  2 years ago  
I have a little problem. I am working on an application that is in its finishing stages right now and there are only a little minor issues that I need to solve. When someone clicks a button I want to notify him that the action was completed / or not. The best way to do is by using message box. 
javax.swing.JOptionPane.showMessageDialog(null, "bla bla");

I tryed this one and I can't say that I like the results. Sometimes it works as expected but sometimes it just crashes and doesn't show or it does show but doesn't close, ...

I realy want something easy to impelent - with a few lines of code because I just need to show the message and close. I don't need anything else. Any ideas on what I could use? Or maybe I can somehow make the swing version work?

Replies(8)

Search the forum for JOptionPane, you can get some hints.
Swing doesn't mix well with Processing, because of different threading systems. Some people had success in using JOptionPane by stopping the drawing (noLoop()), then resuming it on return.
I did search for many things. I couldn't find anything useful. I found the trick with noLoop too problem is that it doesn't work. It makes everything worse. The messageBox never loads and the whole application hangs.
Well, give up on Swing, then, and just display the message on the screen.
Alternatively, GUI libraries like G4P can display secondary windows.
Well I am using Control P5 in my sketch, but displaying a new window and then displaying the message in it seems a little too complicated. It isn't hard to do but I am looking for a better way.
hi, have a look at the ControlP5displayOnTop3D example (included in the examples folder that comes with controlP5), maybe this gives you a start. 
Overkill!
The only question with this is - is it possible to set static width/height for a textlabel. And then center the text in that label? So if the text is too long it gets into a new line and positions itself in the center not on the left. Displaying windows isn't a problem.
I am still having trouble with this thing for some reason. I can show a new ControlP5 ControllWindow with a TextLabel and it shows up nicely. But I can't get it to have a line break if the text is too long and to center the text. The Label in the ControlP5 library seems to have an option multiline, but the TextLabel doesn't. And I have no idea how I could show the Label inside the controlWindow.
I managed to do something. Sadly I have to manualy add \n characters into the message.

Copy code
  1. private void showBox (final String sporocilo)
  2. {
  3.   final PFont fontek = loadFont("ArialTest.vlw");  
  4.      
  5.   //Izracunamo x in y pozicijo (okno se pojavi na sredini klicujocega okna) ter nastavimo visino in sirino okna
  6.   Rectangle r = frame.getBounds();
  7.   final int visina = 150;
  8.   final int sirina = 400;
  9.   final int x = r.x+r.width/2-sirina/2;
  10.   final int y = r.y+r.height/2-visina/2;
  11.   
  12.       
  13.   ControlP5 boxControl = new ControlP5(this);
  14.   boxControl.setAutoDraw(true);
  15.   ControlWindow boxWindow = boxControl.addControlWindow("boxWindow", x, y, sirina,visina);
  16.   boxWindow.setTitle("Sporocilo");
  17.   boxWindow.hideCoordinates();
  18.   boxWindow.setBackground(color(#6386c4));

  19.   
  20.   
  21.   Textlabel boxLabel = boxControl.addTextlabel("boxLabel", sporocilo, 10,10);

  22.   boxLabel.setWidth(350);
  23.   boxLabel.moveTo(boxWindow);
  24.   boxLabel.setPosition(10,30);
  25.    
  26.    
  27.   boxControl.setColorActive(#79FFB8);
  28.   boxControl.setColorForeground(#a8e6c6);

  29.   Button gumb = boxControl.addButton("OK_message",0,160,100,80,25);
  30.   gumb.captionLabel().set("OK");
  31.   gumb.captionLabel().style().marginLeft = 27;
  32.   gumb.moveTo(boxWindow);


  33.   boxControl.setControlFont(fontek);

  34.    
  35.    
  36. }