0

I've a problem that is driving me crazy.

I have a classic JTable, with several columns. I need that a particular column, instead of simple texts values in its cell, it must contain a ComboBox. I searched A LOT, all I found was examples that would implement the same JComboBox in each cell of the column, that it's not what I need: I need that each cell of the column has a combo box with different values.

Can anyone give me some practical example of how to do it, please?

Thanks.

PS: I'm using NetBeans.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
aur0n
  • 463
  • 3
  • 14
  • 25
  • 2
    See: http://stackoverflow.com/questions/4211452/how-to-add-unique-jcomboboxes-to-a-column-in-a-jtable-java for one way. – camickr Feb 13 '12 at 18:33
  • Can you add an example of what you want to make your request clearer? – Alex B Feb 13 '12 at 18:33
  • The above example shows an approach using an ArrayList. If you need something more dynamic then maybe you need to use a Hashmap instead. Then you can map a row number to a combo box model. When you dynmically add rows you then also dynmaically update the hash map as well. – camickr Feb 15 '12 at 15:27

1 Answers1

2

The TableCellEditor.getTableCellEditorComponent() method takes a row as argument. Use the existing example as a guide, and use the row argument of this method to decide which values must be proposed by the combo box.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hello JB Nizet, thanks for the fast reply. I tried to do some experiment, here's what I've got so far: [link]http://pastebin.com/vTfzAS5x Of course it's not working, I got two errors. One saying that GetTableCellEditorComponent can't be referenced in a static context (but it isn't!!), the other is "cannot find symbol setCellEditor(javax.swing.DefaultCellEditor". Please, can you help me through this? Would be much appreciated. – aur0n Feb 13 '12 at 19:46
  • @aur0n: [`TableComboBoxByRow`](http://stackoverflow.com/a/3256602/230513) is an example. – trashgod Feb 14 '12 at 04:25
  • Thank you trashgod, that was quite useful. I managed to get it to work, but I have two questions: I tried to dynamically add more rows with specific combobox, but it doesn't work on new added rows? I tried several times with different methods. ;-( Suggestions? – aur0n Feb 14 '12 at 18:25
  • @aur0n, Why do you ask questions if you don't pay attention to all the comments/answers? I gave a link to that code 8 hours before the link in this answer. – camickr Feb 14 '12 at 21:48
  • Sorry @camickr, it's the first time I post on stackoverflow, so I'm a bit disoriented. I do apologize. Back on the problem, still no fix. Is there way to dynamically add new rows with specific ComboBoxes, with your piece of code? – aur0n Feb 15 '12 at 08:34