小编典典

无论索引如何,总是从DefaultTableModel中删除最后一行

java

当我尝试从Java表中删除行时遇到一些问题。特别是,我使用DefaultTableModel,并且当我尝试删除该行时,使用removeRow(introw)方法,无论是什么,都将删除最后一行row。例如,假设我们有六行。当removeRow(0)removeRow(2)removeRow(5)叫,最后一行是始终去除。任何想法,为什么会这样?

谢谢

-更新

当按下jtable的特定单元格时,应删除相应的行。

    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);
        }
    }   
}

-–更新2号这是我的问题的一些代码。我删除了一些不相关的内容。

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);
}
}

阅读 213

收藏
2020-09-08

共1个答案

小编典典

所述row从得到的columnAtPoint()是在 坐标,而removeRow()假定 模型
坐标。引用相关教程部分

除非通过排序,过滤或用户对列的操作重新排列了查看的数据,否则区别并不重要。

如果是这样,您将需要使用convertRowIndexToModel()
排序和过滤

末尾介绍的,它建议:

使用分类器时,请始终记住要转换单元格坐标。

另外,请考虑使用ListSelectionListener代替MouseAdapter

附录:您的实现getValueAt()继续访问提供给构造函数的数组,而模型的数据实际上存储在父实现中。如果你真的需要在模型的数据结构的更多控制,扩展AbstractTableModel

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);
            }
        });
    }
}
2020-09-08