• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

ComboBox

Hi Experts World!!

Please take a look at these two classes and tell me why a call the combox is not poping up

=============== FIRST CLASS =================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class DiceGame {
   
    //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() {
            String dieType =  (new ChooseTypeOfDie()).getDieType();
            //int  numOfSide = (new GetNumberOfSides()).getNumOfSides();
         //   if(dieType.equals("aRegularDie"))
          //  (new DicePanel(dieType, numOfSide)).createAndShowGUI();
            //d.createAndShowGUI();
           
            }
        });
       
    }
   
}
============================= SECOND CLASS ==========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author  Agbeko Komla
 */
public class ChooseTypeOfDie extends JPanel {
   
    private static final String dieTypes[] = { "aRegularDie", "aLoadedDie"};
    private static String selectedType;
   
    /** Creates a new instance of ChooseTypeOfDie */
    public ChooseTypeOfDie() {  
        JComboBox dies = new JComboBox(dieTypes);
        selectedType =  (String)dies.getSelectedItem();  
        dies.setVisible(true);
        //setVisible(true);
 
    }
   
    public String getDieType(){
        return selectedType;
    }
   
}


Thanks,
Komlaaa
0
komlaaa
Asked:
komlaaa
  • 2
2 Solutions
 
gnoonCommented:
The combobox should be added to a window, then show the window.
0
 
valcarCommented:
You need to put the JPanel within some window to display it.  In this case you probably want to use a JDialog, and, in fact, you should probably change ChooseTypeOf Die to extend JDialog.  Then just don't forget to call show() on it.

0
 
komlaaaAuthor Commented:
sample code please
thanks
0
 
gnoonCommented:
/* ComboShowDemo.java */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Chooser extends JDialog implements ActionListener
{
     JComboBox combo;
     final String faces[] = { ":-)", ":-(", "<|:-)"};
     String selectedValue;

     Chooser()
     {
        // set this dialog with owner, title, modal
        super(new Frame(), "Please choose a face", true);

        // set dialog's size
        setSize(300,100);

        // set dialog's location to the center of screen
        setLocationRelativeTo(null);

        // get a reference to dialog's content
        Container c = getContentPane();

        // set layout manager of dialog's content
        c.setLayout( new BorderLayout() );

        // new instance of combo with init choices
        combo = new JComboBox( faces );

        // add a listener to the combo to get selected value when changed.
        combo.addActionListener(this);

        JPanel panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );

        // add the combo to a panel for layout purpose
        panel.add(combo);

        // add the panel to the dialog's content
        c.add( panel );
        c.add( new JLabel("(Close me to get the selected face)",JLabel.CENTER), BorderLayout.SOUTH);
     }

     // invoked when the combo is selected
     public void actionPerformed(ActionEvent e)
     {
         // save the selected item
         selectedValue = (String)combo.getSelectedItem();
     }

     public String getSelectedChoice()
     {
         // show this dialog, and wait until user hits close button
         setVisible(true);

         // after this dialog is closed, return the selected value.
         return selectedValue;
     }
}

public class ComboShowDemo
{

    public static void main(String[] args)
    {
        String selectedChoice = new Chooser().getSelectedChoice();
        System.out.println("Selected face: "+selectedChoice);
    }

}
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now