I have a problem with a JComboBox popup. My JComboBox has an autocompletition implementation like google search box.
So, the problem is if I add or remove items when popup is visible it is not resized, I need to close and reopen it. But this fire popupBecomeInvisible and popupBecomeVisible and so i can’t use this events for my real porpouse.
There is a way to “refresh” popup size in according to count of items that it contains, without close and reopen it?
Thanks.
Invoke revalidate() on the panel containing the combobox. This will cause the components to be layed out again based on their preferred sizes.
This is the same concept as adding/removing a component on a visible GUI.
Edit:
Just reread your question. I’m not sure if you can dynamically resize the popup when it is open but you can check out Combo Box Popup. It shows you how to override the preferred width of the popup. This code is executed when the popup menu is about to be shown. But you may be able to use the concepts to access the popup and change the width dynamically.
Edit 2:
Here is an example that shows the basic concept. The popup will adjust its width every 2 seconds. However, I don’t know if this will help with your problem because if you are dynamicallyl adding/removing items from the popup, then you will need to recreate the popup every time the popup is changed. This will probably result in the popup hiding/showing which means you will need to live with a little flicker anyway.