JTable: Clicking from editable to non-editable cells.

Hi, I think JTable (or more accurately the selection model) isn't detecting changes in selection when I click from editable to non-editable fields. If you run the code below you'll notice that if you click from an editable cell to a non-editable cell the selection isn't being picked up by the ListSelectionListener. Is this a bug? Otherwise, I'd love an explanation as to what's gonig on.

Thanks.

class TestTable

{

publicstaticvoid main(String[] args)

{

final JTable table =new JTable(10, 6)

{

publicboolean isCellEditable(int r,int c)

{

return c % 2 == 0;

}

};

((DefaultCellEditor)table.getDefaultEditor(String.class)).setClickCountToStart(1);

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.setCellSelectionEnabled(true);

table.getSelectionModel().addListSelectionListener(new ListSelectionListener()

{

publicvoid valueChanged(ListSelectionEvent e)

{

if (e.getValueIsAdjusting())

return;

System.out.println("selected(r, c) = "+table.getSelectedRow()+","+table.getSelectedColumn());

}

});

JDialog d =new JDialog();

d.add(new JScrollPane(table));

d.pack();

d.setVisible(true);

}

}

[2321 byte] By [bparousisa] at [2007-11-27 6:45:50]
# 1
Ah! Sorry, I was confused about how the ListSelectionListener worked. I thought it detected row & column changes, but I see now that the one I have set up only detects row changes.
bparousisa at 2007-7-12 18:17:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

Even with the column listener I'm having issues...weird. When I click into an editable cell the column selection model doesn't detect it. I'm not using the DefaultCellEditor...I have a custom editor, which uses a JTextField. Anyway, I set it up so that when I do a single click on the cell it goes to edit mode. I accomplished this by overriding the changeSelection method:

public void changeSelection(final int row, final int column, boolean toggle, boolean extend) {

super.changeSelection(row, column, toggle, extend);

// my attempt to avoid editCellAt from being called twice in a row

if (getModel().isCellEditable(row, convertColumnIndexToModel(column)) &&

!(isEditing() && getEditingRow() == row && getEditingColumn() == column))

{

if (editCellAt(row, column))

{

getEditorComponent().requestFocusInWindow();

}

}

}

Any thoughts would be helpful.

bparousisa at 2007-7-12 18:17:59 > top of Java-index,Desktop,Core GUI APIs...