JTable搜索和删除行

时间:2014-08-16 13:52:20

标签: java swing jtable

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

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

如果我在搜索文本字段中输入J​​并按搜索按钮,将显示John和Jane 现在,如果我检查2行并按下删除按钮,然后再次按下搜索按钮, John和Jane将被删除,包括Kate和Ann

如果你们还不明白我在说什么,请运行下面的代码。 按照我提到的步骤,你会看到什么是错的。 帮我。感谢

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

1 个答案:

答案 0 :(得分:2)

由于过滤表格时行号会发生变化。使用原始行号删除正确的行。

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

  

根据视图将行的索引映射到基础TableModel。如果未对模型的内容进行排序,则模型和视图索引是相同的。

示例代码:

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...