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

**URL:** https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124
**Category:** Libraries
**Created:** [June 24, 2020, 8:06am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124 "2020-06-24T08:06:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 24, 2020, 8:06am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/1 "2020-06-24T08:06:32Z")

</div>

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:

```auto
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](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:

```auto
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);

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 24, 2020, 10:20am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/2 "2020-06-24T10:20:01Z")

</div>

> [@Ardubora](#):
>
> `if (theEvent.getController().getName() == "red1") {`

> **[equals() / Reference](https://processing.org/reference/String_equals_.html)**
>
> Compares two strings to see if they are the same. This method is necessary because it's not possible to compare strings using the equality operator (==). Returns true if the strings are the sam…

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

or

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

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

  // ...

}

```

---

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 24, 2020, 11:21am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/3 "2020-06-24T11:21:10Z")

</div>

Neither solution works. Have you tested to code?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 24, 2020, 11:38am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/4 "2020-06-24T11:38:34Z")

</div>

- 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](http://Processing.org/reference/thread_.html)

---

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 24, 2020, 11:53am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/5 "2020-06-24T11:53:41Z")

</div>

> [@GoToLoop](#):
>
> Mixing up library controlP5 w/ JOptionPane seems a weird combo IMO.

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

> [@GoToLoop](#):
>
> As a workaround you may place your JOptionPane code under a separate Thread by calling **thread()** :

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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 24, 2020, 12:31pm UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/6 "2020-06-24T12:31:23Z")

</div>

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

```auto
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](http://Processing.org/reference/switch.html)

---

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 24, 2020, 12:38pm UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/7 "2020-06-24T12:38:38Z")

</div>

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.”

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 24, 2020, 12:46pm UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/8 "2020-06-24T12:46:25Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 25, 2020, 5:42am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/9 "2020-06-25T05:42:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ardubora](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@Ardubora](https://discourse.processing.org/u/Ardubora)
#### Post date: [June 25, 2020, 11:22am UTC](https://discourse.processing.org/t/the-joptionpane-dialog-window-appears-when-pressing-the-wrong-button-on-the-controlp5-gui/22124/10 "2020-06-25T11:22:49Z")

</div>

> [@GoToLoop](#):
>
> ```auto
> void dialog() {
> showMessageDialog(null, 
> "Please assign a keystroke\nPress any button!\n", 
> "Important", 
> ERROR_MESSAGE);
> }
> 
> ```

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