Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

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);
                              }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you should onlyt need to clicxk once, check that you aren;t calling that code twice.
Avatar of sciuriware
sciuriware

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.
<*>
>> System.exit(0);

objects, will this call a shut-down hook if there is one?
Avatar of komlaaa

ASKER


>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();
    }
Avatar of komlaaa

ASKER

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.
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 )
ASKER CERTIFIED SOLUTION
Avatar of gnoon
gnoon
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Accept gnoon's comment as answer.