1

I have a written an AbstractTableModel for my JTable in my application. I see from tutorials that I can make the column to have a combobox by getting the column model and then the specific column for example:

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 how do I do this for a specific cell or a row?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dean
  • 8,668
  • 17
  • 57
  • 86

2 Answers2

3

The JTable’s default implementation is column-based. The only way to change that behavior, if you want to have row or single-cell based choices, is to create a subclass of JTable and override the method public TableCellEditor getCellEditor(int row, int column). Inside your implementation you can use the provided row and column indices to make a different choice. The JTable will always use this method to get the cell editor.

Holger
  • 285,553
  • 42
  • 434
  • 765
2

you would need to use, override

  1. prepareEditor

  2. TableCellEditor(required to synchronize editor and renderer)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    See also [`TableComboBoxByRow`](http://stackoverflow.com/a/3256602/230513), whcih iverride's `getCellEditor()`. – trashgod Sep 11 '13 at 09:47