Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

java - JTable removeRow(), removing wrong row

I have a JTable and I need to remove a row, namely, the selected row.

So first, I get the the table model :

    DefaultTableModel model = (DefaultTableModel) table.getModel();

Then the selected row (if the second row is selected, this returns 1, which is understandable because rows start from zero):

    int selectedRow = table.getSelectedRow();

Then I try to remove the row:

    model.removeRow(selectedRow);

Then I set the table model again:

    table.setModel(model);

What this achieves is removing a completely random row. I simply can't understand why. I have sorted the table at some point, using table.setRowSorter(sorter), but I don't know why that should be a problem. If an SSCCE is absolutely needed please let me know, because I have a lot of code to modify before I can produce one.

NOTE: The values returned by these two lines differ:

    System.out.println(table.getValueAt(selectedRow, 1));
    System.out.println(model.getValueAt(selectedRow, 1));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If is JTable filtered or sorted then you can convert

int modelRow = convertRowIndexToModel(row);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...