Resizing JPopupMenu and avoiding a "flicker" issue
I am trying to implement a search results popup list similar to the style found here:
http://www.inquisitorx.com/
(I'm not trying to implement a Google search, I'm just using this as a rough example of the style I'm working on.)
In any event, I am implementing this by using a JList
contained within a JPopupMenu
which is popped up underneath a JTextField
.
When a user enters search terms, the list changes to reflect different matching results. I then call pack
on the JPopupMenu
to resize it.
This works, however, it creates a slight flicker effect since it is actually hiding the popup and showing a popup. (See the private method getPopup
in JPopupMenu
where it explicitly does this.)
Is the开发者_开发百科re any way to just get it to just resize itself (aside from using a JWindow
)?
to me work :
popup.pack();
popup.validate();
this work in linux with java version "1.6.0_34". I don't know is that work on windows.
The setSize() method didn't work in my case. The popup.pack() approach did work, but also resulted in a flicker. The best solution that I have found was the one found here. In short:
Window window = SwingUtilities.getWindowAncestor(popupMenu);
window.pack();
window.validate();
Have you tried setSize()
? It looks like that gets handled in JComponent
and might avoid the repaint issues.
I think the issue that getPopup
is addressing is what to do when the dimensions of the popup will not fit within the window. When that happens, it drops back from a lightweight component to a heavyweight and that definitely requires a hide and show. So, I think if you can guarantee your popup won't extend beyond the window the setSize might do the trick.
精彩评论