Different combo boxes are cell editors

H

hinz.adam

I would like to use combo boxes in my tables but have different values
in combo boxes that are in the same column. If found the following
example on the java website:

TableColumn sportColumn = table.getColumnModel().getColumn(2);
....
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));


But this example only allows the same combo box for every row. How can
I fix this?

Thanks,
Adam
 
R

Rhino

I would like to use combo boxes in my tables but have different values
in combo boxes that are in the same column. If found the following
example on the java website:

TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));


But this example only allows the same combo box for every row. How can
I fix this?
"Fix" suggests "broken" and I don't think this is broken; the concept
behind columns in a JTable are that each of values in the column are of the
same type and subject to the same renderers and editors. I'd be surprised if
you could set a different editor on different cells of the same column; that
would appear to defy the basic reasoning behind the JTable.

But I could be wrong. The best article I've ever seen on renderers and
editors is at
http://www-128.ibm.com/developerworks/library/j-jtable/. Maybe if you read
that you will get an idea on how to do what you want to do - or a
categorical statement that it can't be done at all. I haven't been through
the article in a fair while so I don't remember all the details of what the
author says.
 
C

Christian Kaufhold

I would like to use combo boxes in my tables but have different values
in combo boxes that are in the same column. If found the following
example on the java website:

But this example only allows the same combo box for every row. How can
I fix this?

Use a custom cell editor, which has different editorComponents for different
rows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top