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

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
AI Thread Summary
The discussion revolves around a coding issue related to a button action in a Java application that adds a color based on user input. The user experiences a problem where, after successfully adding a color, the input dialog reappears as if the button was clicked again. This behavior is attributed to the action listener being added twice: once during button instantiation and again when adding the buttons to the panel. A suggestion is made that the issue may also stem from the input dialog returning an empty string if the user does not enter a color name. Ultimately, the user resolves the problem by recognizing the duplicate action listener setup.
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.
 
Technology 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 BiGyElLoWhAt
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
2
Views
3K
Replies
3
Views
3K
Back
Top