The JOptionPane dialog window appears when pressing the "wrong" button on the controlP5 GUI

I made a dialog window appear when one is pressing the button “red1”. The first attempt at compiling after restart is normal. When I press “ok” in that pop-up or I close it the button “red1” still stays pressed/active. And it also doesn’t matter if I press something else in the GUI at that point. The dialog always shows up (although it is only in that single if statement.). That is not intended.

The problem must be located here:

if(theEvent.isController()) { 
 
        print("\ncontrol event from : "+theEvent.getController().getName());

        if(theEvent.getController().getName()=="red1") {

            showMessageDialog(null, 
                              "Please assign a keystroke\n"+ "Press any button!\n", 
                              "Important", 
                              YES_NO_CANCEL_OPTION);
        }
 
        if(theEvent.getController().getName()=="red2") {
          
        }
//All the other buttons etc etc..

It might be good to know that my ultimate goal is to synchronise the Software with the Hardware (when it comes to pressing buttons) and to assign custom buttons to the keystrokes of the hardware via the software.

By the way, I also posted something similar on Stack Overflow. Here is a link to that post;

[ https://stackoverflow.com/questions/62548821/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-gui ]

Here is the full code if someone bothers:

import controlP5.*; 

import static javax.swing.JOptionPane.*;


PFont Font;
ControlP5 controlP5;

static final float startRange = -0.00001;
static final float changeEndRange = 100.0000;
static final float loopEndRange = changeEndRange + 0.0001;

void setup(){
    size (500,350);// länge, breite
    smooth();

    controlP5 = new ControlP5(this);

    controlP5.addBang("red1")
    .setPosition(50,50)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xffff0000)
    .setColorActive(0xff8B0000);
 
    controlP5.addBang("red2")
    .setPosition(150,50)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xffff0000)
    .setColorActive(0xff8B0000);
 
    controlP5.addBang("blue1")
    .setPosition(50,150)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xff00BFFF)
    .setColorActive(0xff1E90FF);
 
    controlP5.addBang("blue2")
    .setPosition(150,150)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xff00BFFF)
    .setColorActive(0xff1E90FF);
 
    controlP5.addBang("green1")
    .setPosition(50,250)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xff228B22)
    .setColorActive(0xff006400);
 
    controlP5.addBang("green2")
    .setPosition(150,250)
    .setSize(20,20)
    .setSize(60,60)
    .setColorForeground(0xff228B22)
    .setColorActive(0xff006400);
 
    controlP5.addKnob("knob2")
    .setValue(0)
    .setRange(startRange,loopEndRange)
    .setValue(0)
    .setPosition(375,200)
    .setSize(80,80)
    .setColorBackground(0x00000000)
    .setColorForeground(0xFFFFFF00)
    .setColorActive(0xFFFFFF00);

    controlP5.addKnob("knob1")
    .setValue(0)
    .setRange(startRange,loopEndRange)
    .setValue(0)
    .setPosition(375,70)
    .setSize(80,80)
    .setColorBackground(0x00000000)
    .setColorForeground(0xffff0000)
    .setColorActive(0xffff0000);
}

void controlEvent(ControlEvent theEvent) {


    if(theEvent.isController()) { 
 
        print("\ncontrol event from : "+theEvent.getController().getName());

        if(theEvent.getController().getName()=="red1") {

            showMessageDialog(null, 
                              "Please assign a keystroke\n"+ "Press any button!\n", 
                              "Important", 
                              YES_NO_CANCEL_OPTION);
        }
 
        if(theEvent.getController().getName()=="red2") {
          
        }
 
        if(theEvent.getController().getName()=="blue1") {

        }
 
        if(theEvent.getController().getName()=="blue2") {

        }
 
        if(theEvent.getController().getName()=="green1") {

        }
 
        if(theEvent.getController().getName()=="green2") {
        
        }
 
        if(theEvent.getController().getName()=="knob1Red") {
            println(", value : "+theEvent.getController().getValue());
   
            float loopKnob1 = +theEvent.getController().getValue();
            if(loopKnob1 > changeEndRange){
                theEvent.getController().setValue(0);
                println("\nloopEndRange:"+loopEndRange);
                println("\nchangeEndRange:"+changeEndRange);
            }else if(loopKnob1 < 0){
                theEvent.getController().setValue(changeEndRange);
                println("\nloopEndRange:"+loopEndRange);
                println("\nchangeEndRange:"+changeEndRange);
            }
  

 
   }
 
        if(theEvent.getController().getName()=="knob2Yellow") {
            println(", value : "+theEvent.getController().getValue());
 
            float loopKnob2 = +theEvent.getController().getValue();
            if(loopKnob2 > changeEndRange){
                theEvent.getController().setValue(0);
                println("\nloopEndRange:"+loopEndRange);
                println("\nchangeEndRange:"+changeEndRange);
            }else if(loopKnob2 < 0){
                theEvent.getController().setValue(changeEndRange);
                println("\nloopEndRange:"+loopEndRange);
                println("\nchangeEndRange:"+changeEndRange);
            }
        }
    }
} 

void draw () {
      Font = createFont("Magneto", 17);
      textFont(Font);
      background(23,15,97);
  
      text ("Racing Sim v0.1", 120, 30);

if ("red1".equals(theEvent.getController().getName())) {

or

final String name = theEvent.getController().getName();

if ("red1".equals(name)) {

  // ...

}
1 Like

Neither solution works. Have you tested to code?

  • Mixing up library controlP5 w/ JOptionPane seems a weird combo IMO.
  • Be aware that JOptionPane dialogs are modal; meaning they halt the current Thread.
  • Thus it also halts the controlP5 GUI controls, b/c they’re run by the sketch’s “Animation” Thread as well.
  • As a workaround you may place your JOptionPane code under a separate Thread by calling thread():
    Processing.org/reference/thread_.html
1 Like

I am happy to change to some alternative if you can suggest me something.

Sadly I don’t understand. I am still very much a beginner when it comes to this.

Something like this wouldn’t halt the whole sketch due to the modal dialog:

import controlP5.*; 
import static javax.swing.JOptionPane.*;

void controlEvent(final ControlEvent theEvent) {
  switch (theEvent.getController().getName()) {
  case "red1":
    thread("dialog");
    break;

  case "green2":
    // ...
    break;

  // ...

  case default:
    println("Unknown controller!");
  }
}

void dialog() {
  showMessageDialog(null, 
    "Please assign a keystroke\nPress any button!\n", 
    "Important", 
    ERROR_MESSAGE);
}

However, I’m lost about exactly you’re trying to achieve w/ that JOptionPane dialog.

Processing.org/reference/switch.html

1 Like

I want to use that pop-up to give the user the message to give his custom input for the keystroke.

As I wrote in my initial post “to assign custom buttons to the keystrokes of the hardware via the software.”

My knowledge about Processing’s GUI libraries is not that great.

And I’m also confused about what keystrokes got anything to do w/ GUI mouse clickable buttons?

What I want is a gui of the hardware. (With the software) You choose which button on the hardware will be assigned some button from your keyboard. I hope that’s simple enough. Something similar to Xpadder for example.

I still get the same problem when using your way of doing it. It changes nothing other than that it seems to work faster.