?
Solved

ComboBox and JOptionsPane

Posted on 2004-10-25
4
Medium Priority
?
758 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
Comment
Question by:komlaaa
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12407863
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
ID: 12408023

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
ID: 12408209
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:
aviadbd earned 1200 total points
ID: 12413615
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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month4 days, 10 hours left to enroll

601 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