h1

What is the easiest way to create a custom JComboBox Popup?

May 28, 2009

If you want a swing JComboBox popup that is not a list (say you want to do a calendar) it would be great to be able to just replace the popup used with your own implementation – you can do that by subclassing the look and feel class and do your own implementation of the Combo Box but you will have to do that per look and feel that you support and what if you want to support every look and feel?

Can you help? What is a good suggestion to circumvent the Look and Feel Problem? Your comment is appreciated!

3 comments

  1. I just finished a project doing just that. I needed a color picker combo box. I have also written a date picker too.

    What I ended up doing, after web research – this is not my own idea, was ignoring the default pop up, doing my own via a modeless, frameless (ie no decorations) JDialog. When the arrow button is pressed or the user press the down arrow key I position and show this dialog instead of having the default Combobox list appear.

    Overriding the LnF of the combobox will not give you an interactive control. I needed to have a button for [More colors...] and keyboard interact to pick for one of the standard 40 colors. Similar things were needed for the Calendar control. All it all it works like a champ and is fully keyboard accessible.

    http://evaristo.memoriapersistente.pt/svn/evaristo/trunk/tools-1/src/org/freixas/jcalendar/

    Here is the code I started with. I made heavy modifications because I wanted a different graphical look to things.


    • Great – however, the example you mention above from JCalendar still overwrites all the look and feels and has the problem that if a class from the look and feel is not present (such as if you compile on PC for Mac) then the compilation fails. It would be nice not to have to deal with that issue.


  2. Odd, I don’t have that issue. Maybe it is an older version of the code, I did not have original link but did a quick search on-line for my post.

    I use Eclipse to compile on PC and we test and run on Mac and Linux. I have not had any issues and I have run Windows, Mac, Linux and Nimbus LnF with the two controls I have written. I know there is some code in there playing with LnF but it did not seem to affect me.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.