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]
# 1
> java.lang.ArrayIndexOutOfBoundsException: -1"-1" means no rows/columns are selected.Also, the following lines are not needed. The table will repaint itself once the TableModel is updated.photoList.revalidate();repaint();
camickra at 2007-7-8 10:33:07 > top of Java-index,Desktop,Core GUI APIs...
# 2
Which line does the except come from? Also in valueChanged if row is removed selectedRow and selectedColumn may be -1. Run code in a debugger or use printlns to see those values.Just a thought.DB
dbulaia at 2007-7-8 10:33:07 > top of Java-index,Desktop,Core GUI APIs...
# 3
but the row is selected as the cell renderer changes the selected rows to blue in my program, which it does.
jonesy21a at 2007-7-8 10:33:07 > top of Java-index,Desktop,Core GUI APIs...
# 4

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

jonesy21a at 2007-7-8 10:33:07 > top of Java-index,Desktop,Core GUI APIs...
# 5

> 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

dbulaia at 2007-7-8 10:33:07 > top of Java-index,Desktop,Core GUI APIs...
# 6
as soon as i click on the remove button it does this. the row doesnt get removed
jonesy21a at 2007-7-8 10:33:08 > top of Java-index,Desktop,Core GUI APIs...
# 7
these lines appear when the row is selected:col 0 row 0col 0 row 0which is correct as there is only one column and i have just added a single row.then when i click remove i get this:col 0 row -1col -1 row -1
jonesy21a at 2007-7-8 10:33:08 > top of Java-index,Desktop,Core GUI APIs...
# 8

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);

}

Ahsan.Fayyaza at 2007-7-8 10:33:08 > top of Java-index,Desktop,Core GUI APIs...
# 9

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

jonesy21a at 2007-7-8 10:33:08 > top of Java-index,Desktop,Core GUI APIs...
# 10
> i simply want the selected row to be cyan in color when it has been selectedtable.setSelectedBackground( Color.CYAN );
camickra at 2007-7-8 10:33:08 > top of Java-index,Desktop,Core GUI APIs...