I face some problems when I am trying to remove rows from a table in java. In particular, I use the DefaultTableModel
, and when I am trying to remove a row, using the removeRow(int row)
method, the last row is removed, regardless what the row
is. For example, let's say that we have six rows. When the removeRow(0)
or removeRow(2)
or removeRow(5)
is called, the last row is always removed. Any idea, why this is happening?
Thanks
---update
When a particular cell of the jtable is pressed, the corresponding row should be removed.
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 some code with my issue. I've removed some stuff that are irrelevant.
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);
}
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…