1

I have seen this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

But it defines a single combo box for the entire column. I would like to define different combo boxes for the different cells of the column.

For simplicity's sake, let's assume that there are n rows in the JTable and the i'th column of the JTable will be a column containing combo boxes. If I have a ComboBox[] comboBoxes, where comboBoxes.length == n, how can I resolve that comboBoxes[0] will be the content of the [0][i]'th cell of the JTable, comboBoxes[1] will be the [1][i]'th cell of the JTable and so on, comboBoxes[n - 1] will be the [n - 1][i]'th cell of the JTable. How can I achieve this?

Thanks.

animuson
  • 53,861
  • 28
  • 137
  • 147
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Similar to the way in the tutorial posted, except instead of getting the column and setting the whole column do it just for the once cell. – Java Devil Jun 25 '13 at 05:58
  • You speak from my heart. But how does somebody do that? – Lajos Arpad Jun 25 '13 at 06:06
  • 1
    Check out this [post](http://stackoverflow.com/questions/3256086/jcombobox-in-a-jtable-cell) – Java Devil Jun 25 '13 at 06:12
  • Thank you, but I still do not know how can I set the cell editor of a cell. I have seen in the example that a method was created along with the creation of the new variable, but I do not have the luxury to be able to do this when the object is constructed. Is there a way to link the object with the method later? – Lajos Arpad Jun 25 '13 at 06:40

1 Answers1

2

I still do not know how can I set the cell editor of a cell.

In the particular case of TableComboBoxByRow, the program first creates a series of editors in a List<TableCellEditor> named editors for later use. The program then overrides the getCellEditor() method of JTable, and it returns the desired editor for each row in the combo column. Note how the first three rows each get a different editor from the List, while the row four editor is just the default text field provided by the superclass.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Thank you for your answer, getCellEditor overwriting it is. This was the missing piece of puzzle, it works like a charm! Thank you again. – Lajos Arpad Jun 25 '13 at 09:10