Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable delete row problem

 
Mason Zhao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, friends,

I have a JScrollPane contains a JTable(DefaultTableModel). When I delete one row of table, like " tableModel.removeRow(index)", the problem occurs, the tableModel removes row(index), but the row (index+1) on the UI disappears. I am so confused, can anybody give me a hand?
Thanks,
Have a great day!

Mason
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works fine for me, problem must be in your code
 
Mason Zhao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Michael,
I didn't explain clearly. In my JTable every row contains a JPanel, the JPanel contains one label,one editButton, one deleteButton.
when I click on the deleteButton, I want the JTable remove the row(index=JTable.getSelectedRow()) what I clicked, But the results are: the tableModel removes the row(index), the JTable UI removes the row(index+1).

Any suggestion is highly appreciated.

Mason
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mason Zhao:
In my JTable every row contains a JPanel, the JPanel contains one label,one editButton, one deleteButton.
when I click on the deleteButton, I want the JTable remove the row(index=JTable.getSelectedRow()) what I clicked, But the results are: the tableModel removes the row(index), the JTable UI removes the row(index+1).


How are you removing the row? Are you doing it from within your table model or without? Are you remembering to call fireTableRowsDeleted()?
 
Mason Zhao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use DefaultTableModel, do i still need fireTableRowdeleted()?

Thank you.
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mason Zhao:
I use DefaultTableModel, do i still need fireTableRowdeleted()?


The fireTableRowsDeleted() has to be called somehow. If you are
using DefaultTableModel.removeRow() then it calls it for you, and
from your original post is appears that's how you are doing it so
you should be ok.

How exactly are you deleting the row? You mention a delete button
in a JPanel in each table row. So you have a custom cell editor
that calls removeRow(), is that it?
 
Mason Zhao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,Brian ,

According to your suggestion, I modified the component renderer and editor, it
really works.

I appreciate your help.

Mason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic