Solved

ComboBox and JOptionsPane

Posted on 2004-10-25
698 Views
Last Modified: 2008-01-09
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;
    }
}
0
Question by:komlaaa
    4 Comments
     
    LVL 92

    Expert Comment

    by:objects
    there does not appear to be any code to perform an action if the combo selection is changed.
    0
     
    LVL 3

    Expert Comment

    by:aviadbd

    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
     

    Author Comment

    by:komlaaa
    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
     
    LVL 3

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    845 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now