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

x
?
Solved

ComboBox

Posted on 2004-10-25
4
Medium Priority
?
254 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
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 16

Assisted Solution

by:gnoon
gnoon earned 75 total points
ID: 12406596
The combobox should be added to a window, then show the window.
0
 

Accepted Solution

by:
valcar earned 300 total points
ID: 12406610
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
ID: 12406888
sample code please
thanks
0
 
LVL 16

Expert Comment

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

618 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