?
Solved

JOptionPane.showConfirmDialog

Posted on 2004-11-12
11
Medium Priority
?
362 Views
Last Modified: 2008-01-16
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);
                              }
0
Comment
Question by:komlaaa
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12572484
you should onlyt need to clicxk once, check that you aren;t calling that code twice.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12572735
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12574533
>> System.exit(0);

objects, will this call a shut-down hook if there is one?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:komlaaa
ID: 12575037

>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
 

Author Comment

by:komlaaa
ID: 12575075
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12577151
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
 
LVL 16

Accepted Solution

by:
gnoon earned 800 total points
ID: 12581186
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12803884
Accept gnoon's comment as answer.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question