简体   繁体   English

未从JTable中删除的行

[英]rows not deleted from JTable

In my JTable, all my checkboxes are in column 3. after I onclick a button, it will remove those rows that are checked. 在我的JTable中,我的所有复选框都在第3列。在我点击一个按钮后,它将删除那些被检查的行。

But however after implementing the functions in my onclick. 但是在我的onclick中实现这些功能之后呢。 It does not remove the the rows. 它不会删除行。

The method for the button listener 按钮监听器的方法

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

and this is what is in my AbstractTableModel class 这就是我的AbstractTableModel类中的内容

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

You need to remove the row from the actual data in your model (ie the array data). 您需要从模型中的实际数据中删除该行(即数组数据)。 fireTableRowsDeleted is not enough. fireTableRowsDeleted是不够的。 That just updates ui stuff. 这只是更新ui的东西。 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. 更好地使用List来更容易操作。 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. 因此,如果删除一行,则还需要减少循环中的行。

Also as @AndrewThompson mentioned, just use a DefaultTableModel . 同样如@AndrewThompson所提到的,只需使用DefaultTableModel I don't see anything special about your model that warrants the need for a custom model. 我没有看到任何关于您的模型的特殊情况,因为需要定制模型。

Here's an example with DefaultTableModel 这是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();
            }
        });
    }
}

Can you try with DefaultTableModel that works perfectly. 你可以尝试使用完美的DefaultTableModel Override other methods of JTable if needed. 如果需要,覆盖JTable其他方法。

sample code: 示例代码:

DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model) {
    @Override
    public Class<?> getColumnClass(int column) {
        switch (column) {
            case 2:
                return Integer.class;
            case 3:
                return Boolean.class;
            default:
                return String.class;
        }
    }

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

To remove row call 删除行调用

((DefaultTableModel) table.getModel()).removeRow(row);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM