未从JTable中删除行 [英] rows not deleted from JTable

查看:104
本文介绍了未从JTable中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JTable中,我所有的复选框都在第3列中.单击按钮后,它将 删除那些被选中的行.

In my JTable, all my checkboxes are in column 3. after I onclick a button, it will remove those rows that are checked.

但是在实现我的onclick函数之后.它不会删除行.

But however after implementing the functions in my onclick. It does not remove the the rows.

按钮监听器的方法

class DeleteBtnListener implements ActionListener {  
    @Override
    public void actionPerformed(ActionEvent arg0) {
        for(int row = 0; row < table.getRowCount(); ++row) {
            if((Boolean) table.getValueAt(row, 3) == true) {
                myTableModel.removeRow(row);
                table.revalidate();
                table.repaint(); 
            }
        }       
    }
}

这就是我的AbstractTableModel类

and this is what is in my AbstractTableModel class

@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel {
    private final boolean DEBUG = true;

    private String[] columnNames = {"Name",
            "Age",
            "Salary",
    "Delete"};

    private Object[][] data = {
            {"Kathy", "20",new Integer(5), new Boolean(false)},
            {"John", "35", new Integer(3), new Boolean(false)},
            {"Sue", "20", new Integer(2), new Boolean(false)},
            {"Jane", "12", new Integer(20), new Boolean(false)},
            {"Mary", "42", new Integer(10), new Boolean(false)}
    };

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    @SuppressWarnings("unchecked")
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value);

        }
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

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

推荐答案

您需要从模型中的实际数据(即数组数据)中删除该行. fireTableRowsDeleted是不够的.那只是更新ui的东西.但是请记住,您正在使用数组.因此,您将需要根据数据相应地推进行索引.更好地使用列表以便于操作.另外请记住,您正在尝试同时"删除循环中的行.因此,如果删除一行,则还需要在循环中减少该行.

You need to remove the row from the actual data in your model (i.e. the array data). fireTableRowsDeleted is not enough. That just updates ui stuff. Keep in mind though, you are using an array. So you will need to advance the row indices accordingly with the data. Better use a List for easier manipulation. Also keep in mind you are trying to remove rows "concurrently" in the loop. So if you remove a row, you need to decrement the row in the loop also.

就像@AndrewThompson提到的那样,只需使用 DefaultTableModel .我认为您的模型没有什么特别之处,因此不需要自定义模型.

Also as @AndrewThompson mentioned, just use a DefaultTableModel. I don't see anything special about your model that warrants the need for a custom model.

以下是 DefaultTableModel

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTable {


    private DefaultTableModel myTableModel = getModel();
    private JTable table = new JTable(myTableModel);

    public TestTable() {
        JButton button = new JButton("Remove All");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int row = 0; row < table.getRowCount(); ++row) {
                    if((Boolean) table.getValueAt(row, 3) == true) {
                        System.out.println("true");
                        myTableModel.removeRow(row);
                        row--;
                    }
                } 
            }
        });

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table));
        frame.add(button, BorderLayout.PAGE_END);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private DefaultTableModel getModel() {
        String[] columnNames = {"Name",
                "Age",
                "Salary",
        "Delete"};

        Object[][] data = {
                {"Kathy", "20",new Integer(5), new Boolean(false)},
                {"John", "35", new Integer(3), new Boolean(false)},
                {"Sue", "20", new Integer(2), new Boolean(false)},
                {"Jane", "12", new Integer(20), new Boolean(false)},
                {"Mary", "42", new Integer(10), new Boolean(false)}
        };
        return new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int col) {
                switch(col) {
                case 0: 
                case 1: return String.class;
                case 2: return Integer.class;
                case 3: return Boolean.class;
                default: return Object.class;
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestTable();
            }
        });
    }
}

这篇关于未从JTable中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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