Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Popup appearing twice. Why?

  1. Feb 16, 2017 #1


    User Avatar
    Gold Member

    Code (Text):
            else if(e.getSource() == add)
                    int redInt = Integer.parseInt(red.getText());
                    int greenInt = Integer.parseInt(green.getText());
                    int blueInt = Integer.parseInt(blue.getText());
                    String s = (String)JOptionPane.showInputDialog("Enter Your Color Name");

                    colorPane.addColor(redInt, greenInt,blueInt, s);
                catch(Exception ex)
                    System.out.println("You must enter a number in all fields!");              
    I call this with a button (add) and it all works fine, however, when I hit ok, it adds the color, but brings the menu popup back as if I had just hit the add button. If you hit ok the second time, though, it goes away. What am I missing? If you need any other code snippets let me know.
  2. jcsd
  3. Feb 17, 2017 #2


    User Avatar
    Gold Member

    Disregard. I added my action listener twice. Once when I instantiated the buttons and a second time when I added the buttons to the panel.
  4. Feb 17, 2017 #3


    Staff: Mentor

    The try block is expecting you to enter a string for the color name. Are you doing this? Hitting the OK button could be setting s to an empty string, which could be causing problems with the addColor() function.

    Edit: Never mind, as the problem seems to be figured out.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted