Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ComboBox and JOptionsPane

Posted on 2004-10-25
4
Medium Priority
?
740 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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