combo box in table column renderer

I've looked extensively on this forum and found a couple topics that should answer my question, but in each one of them, the same link appears and that link is dead.

What I'm trying to do is have a table column renderer with a JLabel as the title and a combo box. I've already made the renderer using a JPanel that I can pretty much add anything to. My problem is that it looks perfect, but nothing happens when I click the combo box, i.e. I can't interact with it; it's basically just a picture.

I also have a few special conditions for this combo box.

1. I have an array of strings that changes constantly. Everytime I click the "down" arrow of the combo box, I want the combo box to have the strings that were in that array when I click it.

Is there some kind of listener for that drop down button? Ideally, I would put the following pseudocode in the actionListener for that drop down button:

- clear the combo box

- add each string in the array to the combo box

2. The combo box itself has to be editable, like a textbox. I want the box itself to be completely independent from the drop down button. For example, When I click one of the elements from the list, I do NOT want the actual combo box to populate with that item.

It would be nice if I could have a textbox and a drop down button next to each other, that physically looks like a combo box and acts like a combo box (i.e. with a drop down menu with the width of the two), yet the two are completely separate from one another.

Instead, I want a special action to happen every time I select one of the items. Let's say it's to System.out.println(selectedItem). Is there a selection listener for the

Sorry for the long winded post, I just wanted to explain myself as clearly as possible. I'll be glad to make any further clarifications and

Message was edited by:

xliu

[1921 byte] By [xliua] at [2007-10-3 3:38:52]
# 1

You also should need to install a 'cell editor' that use the combo box as its editor component.

Renderer does not produce real GUI component. It is just a rubber stamp.

See the article and source code in the tutorial linked from JTable class documentation.

Also the source code for SwingSet2 demo bundled with JDK would help.

hiwaa at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 2

hey, thanks for the reply.

Actually, in simpler terms, what I'm aiming for is a column header with a permanent title, a textfield that can be used to to type things in, and a button that when clicked, takes a certain array of strings and opens up a combo box-style menu with that array. When I then click one of the items in that combo box, I want it to System.out.println the String that I chose. I don't want to edit the entire header.

Really appreciate the help

xliua at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 3

First create the desired JComponent without thinking of its use in a JTable.

Test it rapidly within a JFrame.

When you have achieved the desired behaviour, then (and only then) modify its class to implement the JTable's needed interfaces (actually TableCellRenderer and TableCellEditor).

As your specific component is composed of two focusable sub-components, you will probably have to manage the inner focus yourself.

Perhaps saving the mouse click location and passing it to the cell editior before the editing starts would be a solution.

Franck_Lefevrea at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 4
very nice advice. I'll make a standalone version in a separate JFrame and let you know how it goes. Not very clear about the"modify its class to implement the JTable's needed interfaces (actually TableCellRenderer and TableCellEditor)."part though.thanks a lot!
xliua at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

> very nice advice. I'll make a standalone version in a

> separate JFrame and let you know how it goes.

You're welcome. Feel free to expose any problem you could get after that.

> Not very clear about the "modify its class to implement the JTable's needed

> interfaces (actually TableCellRenderer and TableCellEditor)" part though.

>

This could help you: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

Franck_Lefevrea at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 6

alright, I've succeeded in making a single combo box (instead of the separate text field and button that I envisioned earlier) that fit all my requirements. I did this by overriding some methods in DefaultComboBoxModel and BasicComboBox Editor.

So right now I basically have a single custom combo box. Now all I need to do is have a table header with a permanent label and this combo box. That I can interact with.

Thanks for the link but I've already read through the whole thing : ( It seems they suggest setting the cell editor. Problem is, i don't see any setCellEditor methods in the JTableHeader class or any other class, it only sets the editor for the cells in a column. Also, I'm not even trying to edit the header. For all intents and purposes the combo box isn't related to the header at all, except for the fact that it is physically located within it.

xliua at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 7

alright, I see what you mean by implementing the Renderer and the Editor. I have a JPanel with a Label for the title and this combo box. However, the only way I can "add" this JPanel to the header is through the "setDefaultRenderer" method. There is no "setDefaultEditor" method for JTableHeaders, so how am I supposed to add the editor part of it?

Message was edited by:

xliu

xliua at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...
# 8
I'm afraid you will have to create your own TableHeader, and overrideits getUI() method to return your ComponentUI implementation...(see TableHeader.getUI() and BasicTableHeaderUI for a start)
Franck_Lefevrea at 2007-7-14 21:34:12 > top of Java-index,Desktop,Core GUI APIs...