无论索引如何,总是从 DefaultTableModel 中删除最后一行 [英] Last row always removed from DefaultTableModel, regardless of index

查看:27
本文介绍了无论索引如何,总是从 DefaultTableModel 中删除最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从 java 中的表中删除行时,我遇到了一些问题.特别是,我使用 DefaultTableModel,当我尝试删除一行时,使用 removeRow(int row) 方法,最后一行被删除,无论是什么row 是.例如,假设我们有六行.当 removeRow(0)removeRow(2)removeRow(5) 被调用时,最后一行总是被删除.知道为什么会这样吗?

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?

谢谢

---更新

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

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

--- 更新 no2这是我的问题的一些代码.我删除了一些不相关的内容.

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

推荐答案

columnAtPoint()得到的rowview坐标中,而 removeRow() 假设 model 坐标.引用相关的教程部分:

The row obtained from columnAtPoint() is in view coordinates, while removeRow() assumes model coordinates. Quoting from the relevant tutorial section:

除非您查看的数据已通过排序、过滤或用户对列的操作重新排列,否则这种区别无关紧要.

This distinction does not matter unless your viewed data has been rearranged by sorting, filtering, or user manipulation of columns.

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

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

使用分拣机时,请务必记住转换单元格坐标.

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

另外,考虑使用 ListSelectionListener 而不是 MouseAdapter.

附录:您的 getValueAt() 实现继续访问提供给构造函数的数组,而模型的数据实际上存储在父实现中.如果您确实需要对模型的数据结构进行更多控制,请扩展 AbstractTableModel,如此处所示.

Addendum: Your implementation of getValueAt() continued to access the array supplied to the constructor, while the model's data was actually stored in the parent implementation. If you really need more control over the model's data structure, extend 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);
            }
        });
    }
}

这篇关于无论索引如何,总是从 DefaultTableModel 中删除最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆