removing from a JTable problem
ok when removing from a JTable im gettin this error:
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: -1
the code for my remove button is:
removeGroup =new JButton("remove group");
removeGroup.setPreferredSize(new Dimension(50, 25));
removeGroup.setAlignmentY(CENTER_ALIGNMENT);
removeGroup.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
int rowIndex = photoList.getSelectedRow();
((DefaultTableModel) photoList.getModel()).removeRow(rowIndex);
int rowCount = ((DefaultTableModel) photoList.getModel()).getRowCount();
//No groups left, disable firing
if (rowCount == 0){
removeGroup.setEnabled(false);
}else{
}
photoList.revalidate();
repaint();
newGroupCounter--;
}
});
and here is the code for the TableListener that seems to be causing problems:
publicvoid valueChanged(ListSelectionEvent e)
{
if(!e.getValueIsAdjusting())
{
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
CustomTableCellRenderer renderer =
(CustomTableCellRenderer)table.getCellRenderer(row, col);
renderer.setNewBgColor(Color.cyan, row, col);
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.fireTableCellUpdated(row, col);
}
}
TIA
[2314 byte] By [
jonesy21a] at [2007-10-1 2:03:20]

the exception comes from the lines:
CustomTableCellRenderer renderer =
(CustomTableCellRenderer)table.getCellRenderer(row, col);
and:
model.fireTableCellUpdated(row, col);
the following prints came from running the program with only one row in one column
row = 0, col = 0
row = 0, col = 0
then once i selected the row it spat out:
row = -1, col = 0
then
row = -1, col = -1
> the exception comes from the lines:
>
> CustomTableCellRenderer renderer =
> (CustomTableCellRenderer)table.getCellRenderer(row
> , col);
>
>
> and:
>
> model.fireTableCellUpdated(row, col);
>
> the following prints came from running the program
> with only one row in one column
>
> row = 0, col = 0
> row = 0, col = 0
>
> then once i selected the row it spat out:
>
> row = -1, col = 0
> then
>
> row = -1, col = -1
Was this after you removed the row?
DB
hey,
this is who i recently handled Jtable stuff, look at my code snippets
private int selectedRow = -1;
jPermissionTbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Ask to be notified of selection changes.
ListSelectionModel rowSM = jPermissionTbl.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if( lsm.isSelectionEmpty())
selectedRow = -1;
else
selectedRow = lsm.getMinSelectionIndex();
}
});
-
private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(selectedRow!=-1)
setPerTblModel.removeRow(selectedRow);
}
thanks a bunch. that helped a lot. just one more thing, when the row is selected i added this piece of code:
CustomTableCellRenderer renderer = (CustomTableCellRenderer) photoList.getCellRenderer(selectedRow, 0);
renderer.setNewBgColor(Color.cyan, selectedRow, 0);
but i am gettin a class cast exception with the first line. customTableCellRenderer is my own class which extends DefaultTableCellRenderer. i simply want the selected row to be cyan in color when it has been selected. that was working before but isnt now