Question

I've created an app with a small window (the size of a combo box). I need to create a floating panel that sits outside the window, next to the selected item in a JComboBox. (See attached image).

alt text

I've been reading about the JComboBox.setRenderer(customRenderer) etc. But was just wondering before I go down this path, whether it is at all possible to render something outside the window. I suspect it is, as the combobox itself manages to render it's popup list outside the window.

I'm very new to Swing, so any advice would be appreciated.

Was it helpful?

Solution

It's not possible with the custom renderer since Swing components are light weight. That is, Java is given a native window and all the component drawing takes place in that window. In your case, that is the JFrame containing the combo box.

What you can do though is create a new undecorated window and set it's location accordingly and draw whatever you want inside it.

EDIT: When Java needs to paint outside it's window bounds (like the case of pop up messages or combo boxes drop downs) if the component falls inside the bounds it uses the swing light weight mechanism. But if the component falls out side the bounds it is automatically substituted with a awt heavy weight component that has it's own native drawing surface outside the active window.

OTHER TIPS

I've implemented similar idea using combobox renderers and tooltips on them. Content of every item's tooltip can be customized and rendered using HTML. Location of the tooltip can be set outside of the item itself thus creating design very similar to the one presented in your question.

Here is the starting point for you:

http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top