java - JTable 搜索和删除行

标签 java swing jtable

我正在学习如何使用 JTable,但我的搜索和删除功能都存在问题。

从我的代码中,如果我检查第 1 行和第 3 行并按删除按钮,它将完美删除。

如果我在搜索文本字段中输入 J 并按搜索按钮,将显示 John 和 Jane 现在,如果我检查两行并按删除按钮,然后再次按搜索按钮, 约翰和简将被删除包括凯特和安

如果你们还是不明白我在说什么,请运行下面的代码。 按照我提到的步骤操作,您就会看到问题所在。 帮我。谢谢

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TableExample extends JFrame {

    private static final long serialVersionUID = 1L;  
    protected static final boolean DEBUG = false;
    private JTable table;
    TableModel model;
    JPanel panel = new JPanel(new BorderLayout());
    JButton button = new JButton("Delete");
    JTextField searchTextField = new JTextField(15);
    JButton searchBtn = new JButton("Search");
    JPanel flowLayoutPanel = new JPanel(new FlowLayout());
    TableRowSorter<TableModel> sorter;

    public TableExample() {
        String[] columnNames = {"Employer", "Company", "Salary", "Boolean"};
        Object[][] data = {
                {"Kate", "20",new Integer(5000), new Boolean(false)},
                {"John", "35", new Integer(3000), new Boolean(false)},
                {"Ann", "20", new Integer(4000), new Boolean(false)},
                {"Jane", "12", new Integer(4000), new Boolean(false)},
                {"May", "42", new Integer(4500), new Boolean(false)}
        };
        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class getColumnClass(int column) {

                switch(column) {
                case 0: 
                case 1: return String.class;
                case 2: return Integer.class;
                case 3: return Boolean.class;
                default: return Object.class;

                }
            }
        };

        table = new JTable(model) {
            public boolean isCellEditable(int row, int col) {
                return true;
            }
        };

        sorter = new TableRowSorter<TableModel>(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setRowSorter(sorter);
        table.setGridColor(Color.black);
        JScrollPane scrollPane = new JScrollPane(table);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int row = 0; row < table.getRowCount(); ++row) {
                    if((Boolean) table.getValueAt(row, 3) == true) {    
                        ((DefaultTableModel) model).removeRow(row);
                        row--;
                    }
                }
            }
        });

        searchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = searchTextField.getText();
                if (text.length() == 0) {
                    sorter.setRowFilter(null);
                } else {
                    sorter.setRowFilter(RowFilter.regexFilter(text));
                }
            }
        });

        flowLayoutPanel.add(searchTextField);
        flowLayoutPanel.add(searchBtn);
        panel.add(flowLayoutPanel,BorderLayout.NORTH);
        panel.add(scrollPane , BorderLayout.CENTER);
        panel.add(button, BorderLayout.PAGE_END);
        getContentPane().add(panel);
    }

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

            @Override
            public void run() {
                TableExample  frame = new TableExample();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }
        });
    }
}

最佳答案

因为当您过滤表格时行号会发生变化。使用原始行号删除正确的行。

使用table.convertRowIndexToModel(row)获取实际行号。

Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.

示例代码:

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        for(int row = 0; row < table.getRowCount(); ++row) {
            if((Boolean) table.getValueAt(row, 3) == true) {    
                ((DefaultTableModel) model).removeRow(table.convertRowIndexToModel(row));
                row--;
            }
        }
    }
});

Read more...

关于java - JTable 搜索和删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340807/

相关文章:

java - 在模型更新期间保留排序 JTable 中选定的模型行

java - 使用 Nimbus L&F 设置文件选择器颜色

java - 返回 JButtons 操作监听器,以便另一个类可以使用它

jtable - 以编程方式取消选择和编辑 JTable 单元格

java - 在 jtable 中显示数据库

Java Swing : How does using a JTable work?

java - 关于try-catch

java - tomcat 8、如何访问外部java jar库文件

java - 如何在arraylist中搜索字符串

java - tomcat 5.5升级到tomcat 7.0需要重新编译所有JSP文件吗