Jump to content

Selecting and deleting rows in JTable

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I have a GUI with a JTable filled with objects, a Delete button, and a textarea.

When I press the Delete button, it fires actionPerformed and deletes that row from the table. And when I select a row, it fires valueChanged and shows the toString() method's value of the selected object in the textarea.

However, whenever I delete a row valueChanged is also called, giving me an ArrayOutOfBoundsException, I assume because the row is deleted and getselectedRow() now returns -1.

I could use two different private classes, one for ActionListener and one for ListSelectionListener, but since this is for an exam project, we are supposed to put it all into one private class.

Therefore my question is how I avoid firing valueChanged when I delete a row, or how else I can fire an event when I select a row. I would prefer not to use mouseClicked and similar methods.

Right now I'm using this if to check if a row has been selected, as opposed to a column or cell which I don't need to do anything about.

if (e.getSource() == tblPars.getSelectionModel() && tblPars.getRowSelectionAllowed())

{

...

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
dunno if i'm still on time for the exam.. Is it possible to get more , if not all, the code?
So if i understand it right:
-You select a row.
-it auto shows the value due to selecting
-You delete the row.
-Row is gone
-It's still selected and fire the showvalue again --> problem

Is that the case?

You can offcourse put "the search for the value" in a IF(getselectedRow!=-1)
or is that too easy?

If to easy, you perhaps can do a try / catch. so you can catch the ArrayOutOfBoundsException so the program doesn't crash when it occurs.