views:

250

answers:

3

I am trying to create a combo box so that I can put whatever control I prefer within the pop-up, in my specific case a JTree. Having a look at how the JComboBox is implement, the pop-up is really created by the UI delegate. The problem in changing that is that it would need to be re-implemented for each look and feel, which is something I do not want to do...

I basically want a component that it has the look and feel of a JComboBox (in the current look and feel) and the popup is a JTree (in the current look and feel).

What's the easiest way to do that?

+1  A: 

JComboBox itself can't do what you want. If you're absolutely wedded to the concept of having it act like a JComboBox, you could make a JButton pop up a JPanel on click. Then the JPanel could have whatever you want inside it (JTree, etcetera).

Seth
Yeah, that's how I am doing it right now. The problem is that I want to embed this in a JTable as a TableCellEditor, and having a button in there is quite ugly... :-/
Carcassi
A: 

You just need to extend the BasicComboBoxUI and then override required methods like

public static ComponentUI createUI( JComponent c)

and

protected ComboPopup createPopup()

Creating a custom ComboPopup would require you to put some effort where you can't use the BasicComboPopUp because it extends JPopUpMenu

public class BasicComboPopup extends JPopupMenu implements ComboPopup

So in your case you may want to extend JTree and implement ComboPopup.

I'm doubt about "The problem in changing that is that it would need to be re-implemented for each look and feel" part. I don't think that there will be a problem of re-implementation.

The BasicComboPopup looks differently in different look and feels because it is a JPopupMenu which in turn will have UI delegates. So if you simply extend a JTree you should not be having problems with different look and feels.

Varun
A: 

The answer to use a button that pops up a JPanel with a JTree is correct. In response to Carcassi's comment, you can use a custom TableCellRenderer to change it so that it does not look like the traditional button.

Amber Shah