The last line is always removed from DefaultTableModel, regardless of index

I am having some problems when I try to delete rows from a table in java. In particular, I use DefaultTableModel , and when I try to delete a row using the removeRow(int row) method, the last row is deleted, regardless of what row . For example, let's say that we have six lines. When removeRow(0) or removeRow(2) or removeRow(5) is called, the last line is always deleted. Any idea why this is happening?

thanks

--- update

When a specific jtable cell is specified, the corresponding row must be deleted.

  class TagsTableMA extends MouseAdapter { @Override public void mousePressed(MouseEvent e){ Point p = e.getPoint(); int row = tagsJT.rowAtPoint(p); int column = tagsJT.columnAtPoint(p); if (column == COLUMN_DELETE_TAG){ DocDialog docDialog = new DocDialog(parentMainJF, true, null, "Please confirm...", "Are you sure you want to delete the \"" + tagsJT.getValueAt(row, COLUMN_TAG_NAME) + "\" tag?", DocDialog.TYPE_YES_NO); docDialog.show(); int answer = docDialog.getAnswer(); if (answer == DocDialog.YES) model.removeRow(row); } } } 

--- update no2 Here is the code with my problem. I deleted some things that don't matter.

 import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class MainJF extends JFrame { public MainJF(){ this.add(createTagsTable()); setMinimumSize(new java.awt.Dimension(200,400)); } private JTable createTagsTable(){ String[] columnNames = {"", "Tag", "", "", ""}; Object[][] data = new Object[10][columnNames.length]; for (int i=0; i<data.length; i++){ data[i][COLUMN_CHECK] = false; data[i][COLUMN_TAG_NAME] = "Tag " + i; data[i][COLUMN_TAG_ID] = i; data[i][COLUMN_EDIT_TAG] = "edit"; data[i][COLUMN_DELETE_TAG] = "delete"; } model = new TagsSelectionTableModel(columnNames, data); tagsJT = new JTable(model); tagsJT.setRowSelectionAllowed(true); tagsJT.addMouseListener(new TagsTableMA()); return tagsJT; } class TagsSelectionTableModel extends DefaultTableModel{ public TagsSelectionTableModel(String[] columnNames, Object[][] data){ super(data, columnNames); this.columnNames = columnNames; this.data = data; } private String[] columnNames; private Object[][] data; @Override public Object getValueAt(int row, int col) { return data[row][col]; } } class TagsTableMA extends MouseAdapter{ @Override public void mousePressed(MouseEvent e){ Point p = e.getPoint(); int row = tagsJT.rowAtPoint(p); int column = tagsJT.columnAtPoint(p); if (column == COLUMN_DELETE_TAG){ int irow = tagsJT.convertRowIndexToView(row); model.removeRow(irow); } } } private JTable tagsJT; private TagsSelectionTableModel model; private static int COLUMN_CHECK = 0; private static int COLUMN_TAG_NAME = 1; private static int COLUMN_TAG_ID = 2; private static int COLUMN_EDIT_TAG = 3; private static int COLUMN_DELETE_TAG = 4; public static void main(String args[]) { new MainJF().setVisible(true); } } 
+4
source share
2 answers

row obtained from columnAtPoint() is in the field of view of coordinates, and removeRow() takes the coordinates of the model. Quote from the section:

This difference does not matter if your browsing data has not been rearranged by sorting, filtering, or manipulating user columns.

If so, you will need to use convertRowIndexToModel() , described near the end of Sorting and Filtering , which advises:

When using the sorter, always remember to translate the cell coordinates.

Also, consider a ListSelectionListener instead of a MouseAdapter .

Addendum: your implementation of getValueAt() continued to access the array provided by the constructor, while the model data was actually stored in the parent implementation. If you really need more control over the model data structure, stretch the AbstractTableModel , as shown here .

 import java.awt.EventQueue; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; /** @see https://stackoverflow.com/a/11237205/230513 */ public class MainJF extends JFrame { private JTable tagsJT; private TagsSelectionTableModel model; private static int COLUMN_CHECK = 0; private static int COLUMN_TAG_NAME = 1; private static int COLUMN_TAG_ID = 2; private static int COLUMN_EDIT_TAG = 3; private static int COLUMN_DELETE_TAG = 4; public MainJF() { this.add(new JScrollPane(createTagsTable())); pack(); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } private JTable createTagsTable() { String[] columnNames = {"0", "Tag", "2", "3", "4"}; Object[][] data = new Object[10][columnNames.length]; for (int i = 0; i < data.length; i++) { data[i][COLUMN_CHECK] = false; data[i][COLUMN_TAG_NAME] = "Tag " + i; data[i][COLUMN_TAG_ID] = i; data[i][COLUMN_EDIT_TAG] = "edit"; data[i][COLUMN_DELETE_TAG] = "delete"; } model = new TagsSelectionTableModel(columnNames, data); tagsJT = new JTable(model); tagsJT.setRowSelectionAllowed(true); tagsJT.addMouseListener(new TagsTableMA()); return tagsJT; } class TagsSelectionTableModel extends DefaultTableModel { public TagsSelectionTableModel(String[] columnNames, Object[][] data) { super(data, columnNames); } } class TagsTableMA extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { Point p = e.getPoint(); int row = tagsJT.rowAtPoint(p); int col = tagsJT.columnAtPoint(p); if (col == COLUMN_DELETE_TAG) { model.removeRow(row); } } } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new MainJF().setVisible(true); } }); } } 
+5
source

Looking at the code of the removeRow(int) DefaultTableModel , it is clear that it will remove the array of strings with the corresponding Vector support index:

  public void removeRow(int row) { dataVector.removeElementAt(row); fireTableRowsDeleted(row, row); } 

(from java 6).

I can imagine two possibilities:

  • You have expanded DefaultTableModel and changed the implementation of this method. (I doubt it is - you probably would have allowed it in your post)

  • you have your own renderer for a table that draws cell data based on the row index of the cell.

+3
source

All Articles