Solved

How do I show a Java ComboBox in a popup dialog?

Posted on 2015-02-12
4
72 Views
Last Modified: 2015-02-13
Hi,

I would like to show a combo box in my Java program but I would like show it in a popup dialog box similar to the way I would show a dialog asking for input.

Any idea how to do that?

I know how to add a combobox to the main screen, but that is not what I want: I need it in a dialog separate from the main window.

Thanks
0
Comment
Question by:cschene
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
Comment Utility
Use showInputDialog

Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
                    frame,
                    "Complete the sentence:\n"
                    + "\"Green eggs and...\"",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    icon,
                    possibilities,
                    "ham");

//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
    setLabel("Green eggs and... " + s + "!");
    return;
}

Open in new window

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
0
 

Author Comment

by:cschene
Comment Utility
One question

"Object[] possibilities = {"ham", "spam", "yam"};

I have a variable array that is always different size.

How do I fill the Object in that case with the names of my choices choices
0
 

Author Comment

by:cschene
Comment Utility
Here is my solution based off your answer: works!
solution.java
0
 

Author Closing Comment

by:cschene
Comment Utility
Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 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

6 Experts available now in Live!

Get 1:1 Help Now