Solved

ComboBox

Posted on 2004-10-25
212 Views
Last Modified: 2010-03-31
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
Question by:komlaaa
    4 Comments
     
    LVL 16

    Assisted Solution

    by:gnoon
    The combobox should be added to a window, then show the window.
    0
     

    Accepted Solution

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

    Author Comment

    by:komlaaa
    sample code please
    thanks
    0
     
    LVL 16

    Expert Comment

    by:gnoon
    /* 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Suggested Solutions

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    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

    7 Experts available now in Live!

    Get 1:1 Help Now