ComboBox and JOptionsPane

Hello Experts !!
In this code i am trying to first popup a combobox, make a selection. After the selection a JOptionPane should pop, the actual Dice game should popup. Please take a look to see why combobox is coming. Also when a slection is made, nothing is happening.

================ Main =======================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class DiceGame {
   
       private static String dieType ;
       private static int  numOfSide;
      // private
    //private static DicePanel d = new DicePanel();
    //private static ChooseTypeOfDie  c = new ChooseTypeOfDie();
    //private static GetNumberOfSides g = new GetNumberOfSides();
   
    public static void main(String[] args) {
       
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
           dieType =  (new ChooseTypeOfDie()).getDieType();
          if(dieType.equals("a regularDie")){            
             numOfSide = (new GetNumberOfSides()).getNumOfSides();
            (new DicePanel(dieType, numOfSide)).createAndShowGUI();
            }
            }
        });
       
    }
   
}
=================== Code for combo ==================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class ChooseTypeOfDie extends JFrame{
 
    private static String selectedType;
    private JComboBox dice;
    private final String dieTypes[] = { "a regularDie", "a loadedDie"};
    private static GetNumberOfSides G;
    private static DicePanel D;
     private static String dieType ;
       private static int  numOfSide;
    /** Creates a new instance of ChooseTypeOfDie */
    public ChooseTypeOfDie() {  
        super("Select a Die");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        dice = new JComboBox(dieTypes);
        dice.setBackground(Color.white);
        dice.setMaximumRowCount(2);
        dice.addItemListener(new ItemListener()
        {
         public void itemStateChanged(ItemEvent e){
                selectedType = (String)dice.getSelectedItem();
               
            }
        });
           
        c.add(dice);
        setSize(350,100);
        show();
       
    }
   
    public String getDieType(){
        return selectedType;
    }
   
    public void actionPerformed(ActionEvent e) {
    }
   
}
=================== code for input ====================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


/**
 *
 * @author  Agbeko Komla
 */
public class GetNumberOfSides extends JOptionPane {
   
    private static int num;
    /** Creates a new instance of GetNumberOfSides */
    public GetNumberOfSides() {
       
        String numText = JOptionPane.showInputDialog(null,
                                                      "Enter Number of Side?");
       
       
       try {
                num = Integer.parseInt(numText) ;
            }
       catch(NumberFormatException e)
            {
               
             if( (numText == null) || (numText.trim().equals("")))
            {
           JOptionPane.showMessageDialog (null, "Input must be an integer > 4");
                System.exit(1);
             }
            }
    }
   
    public int getNumOfSides(){
        return num;
    }
}
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:
there does not appear to be any code to perform an action if the combo selection is changed.
0
aviadbdCommented:

From what I see, it seems like the Runnable you pass to "invokeLater" is actually executed before you select the item in the combobox.

What I think you should do is pass the contents of the Runnable to the ComboBox ItemListener itself, or to an ActionListener if its more suitable.

AviadBD.
0
komlaaaAuthor Commented:
After i change it to what u suggested, everything work fine BUT inputbox come popup back and does not let me play
the game that a call to (new DicePanel(selectedType, numOfSide)).createAndShowGUI();
is supposed to show present. Is there anyway i can avoid the input box to popup twice(before and after the main window pop)?
================ CURRENT MAIN ======================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class DiceGame {
   
     //  private static String dieType ;
    //   private static int  numOfSide;
      // private
    //private static DicePanel d = new DicePanel();
    //private static ChooseTypeOfDie  c = new ChooseTypeOfDie();
    //private static GetNumberOfSides g = new GetNumberOfSides();
   
    public static void main(String[] args) {
       
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
           (new ChooseTypeOfDie()).getDieType();
         
            }
        });
       
    }
   
}
==================== CURRENT COMBO CODE =======================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class ChooseTypeOfDie extends JFrame{
 
    private String selectedType;
    private JComboBox dice;
    private final String dieTypes[] = { "a regularDie", "a loadedDie"};
    //private static GetNumberOfSides G;
    //private static DicePanel D;
    // private static String dieType ;
       private static int  numOfSide;
    /** Creates a new instance of ChooseTypeOfDie */
    public ChooseTypeOfDie() {  
        super("Select a Die");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        dice = new JComboBox(dieTypes);
        dice.setBackground(Color.white);
        dice.setMaximumRowCount(2);
        dice.addItemListener(new ItemListener()
        {
         public void itemStateChanged(ItemEvent e){
                selectedType = (String)dice.getSelectedItem();
                 if(selectedType.equals("a regularDie")){            
             numOfSide = (new GetNumberOfSides()).getNumOfSides();
             
            (new DicePanel(selectedType, numOfSide)).createAndShowGUI();
            }
               
            }
        });
           
        c.add(dice);
        setSize(350,100);
        show();
       
    }
   
    public String getDieType(){
        return selectedType;
    }
   
}
0
aviadbdCommented:
Komlaa,

Right before the call to new DicePanel(...) you call new GetNumberOfSides().  What are these two classes, and do they somehow call the input again?

Aviad.
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
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.