Dan Dan - 1 year ago 114
Java Question

refresh setCellEditor for JTable

I was looking for an answer for a previous question and had an ingenious idea to overcome a limit on

. I need the editor to be different on a row by row basis, whereas
can only handle a single editor for each column.

So my idea is to use a
to check the row and column on the
and set new editor each time.

But, calling
a second time do not have any effect. The editor remains to be the first one that was set. So how can I make "setCellEditor" work a second time for the same column?

Here's the code in

public void mouseClicked(MouseEvent e) {
int cols = resultTable.columnAtPoint(new Point(e.getX(), e.getY()));
int rows = resultTable.rowAtPoint(new Point(e.getX(), e.getY()));
StorageObject item = (StorageObject) resultTable.getModel().getValueAt(rows, cols);
TableColumn col = resultTable.getColumnModel().getColumn(cols);
col.setCellEditor(new MyComboBoxEditor(item.list));

Answer Source

I'm not sure why your code isn't working (it's been a while since I've done Swing), but why don't you just override

public TableCellEditor getCellEditor(int row, int column)

On your JTable? Maintain a map of the combo boxes you want to use for each row and in your overriden method return the correct one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download