aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with JOptionPane Combobox max rows or height Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with JOptionPane Combobox max rows or height" Watch "Help with JOptionPane Combobox max rows or height" New topic
Author

Help with JOptionPane Combobox max rows or height

B. Smith
Greenhorn

Joined: Oct 03, 2011
Posts: 8
Hello, all! I'm trying to get a JOptionPane message to display a combo box to select options from, as follows:



The problem is that unlike a manually coded combo box, I have no way to set the maximum row count, as I normally would like this, nor can I make the combo box taller to display more rows at a time as I normally would. The result is a one-line high listbox with a very tiny scroll-bar with over a hundred values in it, which, of course, would be a pain for the end user.

What are my options?

Thanks,

~B. Smith
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Welcome to the Ranch.

JOptionPane#showInputDialog also accepts an Object message.
Set up your JComboBox as you like it and pass it as the message.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Works fine for me using JDK6_7 on XP.

It shows the list with 10 rows:



Post your SSCCE that demonstrates the problem. Maybe its a platform or LAF issue?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Maneesh Godbole wrote:Welcome to the Ranch.

JOptionPane#showInputDialog also accepts an Object message.
Set up your JComboBox as you like it and pass it as the message.

Or a JList wrapped in a JScrollPane.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
B. Smith
Greenhorn

Joined: Oct 03, 2011
Posts: 8
My apologies for the late reply. I am trying to make the SSCE, but removing all "superfluous" code seems to solve the problem. It works in the SSCE I almost posted, giving me a decently tall listbox when I pass in an array of a hundred strings, but in my actual code, it still presents a listbox one entry high. I am now thoroughly confused. When I am able to narrow my problem down to another specific question, I will be back and post in this thread again. Sorry to be a bother.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
The is the point of a SSCCE. Do a simple case and compare to the more complex case. Most times you will find the problem.
B. Smith
Greenhorn

Joined: Oct 03, 2011
Posts: 8
Okay. I found out what the cause was, but I must admit, the behavior is quite odd and confusing.



Apparently, the problem is caused when the first element in the list is an empty string. If you run the above code, you will notice that the first option pane works fine, but the second, with the blank value at the top, causes the list to "shrink" to a single value. This is confusing.

In my project, I can omit the empty string at the beginning of the list. That's not a huge problem. I was going to use it, but there's a workaround I think I could implement. Now, I'm just mystified. Is there anybody here who can explain why this happens, so I can know for next time?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I guess an empty string doesn't have a size which is somehow used by the list renderers. Just make is a string with blanks:



Also, don't mix AWT components in a Swing application. Use a JButton.
B. Smith
Greenhorn

Joined: Oct 03, 2011
Posts: 8
Okay. Thank you so much for the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with JOptionPane Combobox max rows or height