Why is the Popup Menu Appearing Twice When I Add a Color?

  • Topic: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
2 replies · 1K views
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
Code:
        else if(e.getSource() == add)
        {
            try{
                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.
 
Physics news on Phys.org
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.
 
BiGyElLoWhAt said:
Code:
        else if(e.getSource() == add)
        {
            try{
                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.
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.
 
  • Like
Likes   Reactions: BiGyElLoWhAt