JOptionPane.showConfirmDialog

Hi Experts!
With this piece of code, i have to click  ' yes ' botton twice before the dialog closes.
What should change in order to just click 'yes' once.

======================= CODE ======================
    if(!find(selectedGenre) )
                             {                              
                                CdList.setText("");
                                 int r = JOptionPane.showConfirmDialog(null, "No " +
                                 selectedGenre +
                                 " Found Please check back\n " +
                                 "Do you want to make Another Selection?");
                                 if(r != JOptionPane.C .YES_OPTION)
                                    System.exit(0);
                              }
komlaaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you should onlyt need to clicxk once, check that you aren;t calling that code twice.
0
sciuriwareCommented:
You could read your own program better if you wrote it like this:

    if(!find(selectedGenre) )
    {                              
           CdList.setText("");
           int r = JOptionPane.showConfirmDialog
                     (
                            null,
                            "No " + selectedGenre + " Found Please check back\n "
                                   + "Do you want to make Another Selection?"
                     );
                     if(r != JOptionPane.C .YES_OPTION)
                     {
                            System.exit(0);
                     }
    }


You'll find that less confusion in the source leads to less errors.
And I think that 'objects' is right.
<*>
0
Mayank SAssociate Director - Product EngineeringCommented:
>> System.exit(0);

objects, will this call a shut-down hook if there is one?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

komlaaaAuthor Commented:

>You'll find that less confusion in the source leads to less errors
Thanks, sciuriware  I i should improve my code writing format. :)
...
>you should onlyt need to clicxk once, check that you aren;t calling that code twice.
May be i should show the entire code. I did click it once, call it once.

================ code ====================
public class DisplayCdCollection extends JFrame
{
 
    private String selectedGenre;
    private JComboBox categories;
    private JTextArea CdList;
   
    private String genresOnListing[] =
    {"Choose a Catergory" , "All", "R&B", "Blues","Jazz", "Alternative","World",
     "Classical","Country","Reggae","Techno" };
 
    private String genresOnFile[];
    private CDCollection CDs;
     
    public DisplayCdCollection()
       {  
            super("CDs Display");
                                     
            this.setLocation(30,30 );
            this.addWindowListener
            (
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        setVisible(false);
                        dispose();
                        System.exit(0);
                    }
                }
            );
 
            //creating JTextArea to display the CDS
            CdList = new JTextArea();
            CdList.setEditable(false);
           
            //create a contianer with scrollbar where the CdList should go.
            Container c = getContentPane();
            c.setLayout(new BorderLayout());
            c.add(new JScrollPane(CdList), BorderLayout.CENTER);
           
            /**creating combobox to hold the type of CD */
            categories = new JComboBox( genresOnListing );
            categories.setBackground(Color.white);
            categories.setMaximumRowCount(12);
           
            CDs = new CDCollection();
            genresOnFile = CDs.getCdCategoies();
           
            /**
             *add a listner to the combobox. When a genre of CD is chosen a
             *the selection will automatically trigger a processing of a list
             *of CDs of that type.
             */
            categories.addItemListener
            (
                new ItemListener()
                {
                    public void itemStateChanged(ItemEvent e)
                    {      
                       selectedGenre = (String)categories.getSelectedItem();
                       
                              if(find(selectedGenre) )
                              {
                                  if( selectedGenre.equals("All") )
                                    {
                                        CdList.setText("");
                                        CdList.setText( CDs.printAll() );
                                    }
                                  else
                                  {
                                 CdList.setText("");
                                 CdList.setText( CDs.getCDList(selectedGenre) );
                                  }
                              }
                               if(!find(selectedGenre) )
                             {                              
                                CdList.setText("");
                                 int r = JOptionPane.showConfirmDialog
                                 (
                                    null, "No " +selectedGenre +" Found Please check back.\n"
                                    + "Another Selection?"
                                  );

                                 if(r != JOptionPane.YES_OPTION)
                                 {
                                    System.exit(0);
                                 }
                              }
                        }
                    }
               
            ); //end of addItemListener

        //creat a panel for the Cd categories combobox
        JPanel eastPanel = new JPanel();
        eastPanel.add(categories);
        c.add(eastPanel, BorderLayout.EAST);

        setSize(250,100);
        show();
    }
0
komlaaaAuthor Commented:
Or how should i tell JOptionPane.showConfirmDialog to close when i click ' yes ' .
I don't want to System.exit(0), want to still have the main window for another selection.
0
Mayank SAssociate Director - Product EngineeringCommented:
Perhaps the item-state changed event is getting fired twice so that method is called twice. When you click on Yes for the first time, it doesn't exit the program because your condition says if ( r != JOptionPane.YES_OPTION )
0
gnoonCommented:
Yes, the method is fired twice for SELECTED and DESELECTED state change events. Use e.getStateChange() to check which fired event, or use actionlistener instead.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Accept gnoon's comment as answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.