从jtable tablemodel clone中删除一列数据

时间:2016-05-17 11:28:56

标签: java swing jtable tablemodel

我使用rs2xml.jar从数据库填充到jtable的示例数据 我还添加了一个jTextField,通过引用此link来过滤jTable。

我的问题是我将id列填充到jtable,我用它来识别用户编辑时的行和单元格数据。我的问题是如何从TableModel

的克隆中删除id列

代码如下:

Vector originalTableModel = (Vector) ((DefaultTableModel) jTable1.getModel()).getDataVector().clone();

public void searchTableContents(String searchString) {
        DefaultTableModel currtableModel = (DefaultTableModel) jTable1.getModel();
        //To empty the table before search
        currtableModel.setRowCount(0);
        //To search for contents from original table content
        for (Object rows : originalTableModel) {
            Vector rowVector = (Vector) rows;
            for (Object column : rowVector) {
                if (column.toString().contains(searchString)) {
                    //content found so adding to table
                    currtableModel.addRow(rowVector);
                    break;
                }
            }
        }
    }

here is the image of my application

1 个答案:

答案 0 :(得分:0)

首先创建一个文本字段,然后使用下面的代码:

jTextField1.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateFilter(jTextField1.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateFilter(jTextField1.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {//ignore this
            }
        });
    }

    protected void updateFilter(String text) {
        TableRowSorter<TableModel> sorter = (TableRowSorter<TableModel>) jTable1.getRowSorter();
        RowFilter<TableModel, Object> firstFiler = null;
        RowFilter<TableModel, Object> secondFilter = null;
        RowFilter<TableModel, Object> thirdFiler = null;
        RowFilter<TableModel, Object> forthFilter = null;
        List<RowFilter<TableModel, Object>> filters = new ArrayList<RowFilter<TableModel, Object>>();
        RowFilter<TableModel, Object> compoundRowFilter = null;
        try {
            firstFiler = RowFilter.regexFilter(text, 1);
            secondFilter = RowFilter.regexFilter(text, 2);
            thirdFiler = RowFilter.regexFilter(text, 3);
            forthFilter = RowFilter.regexFilter(text, 4);

            filters.add(firstFiler);
            filters.add(secondFilter);
            filters.add(thirdFiler);
            filters.add(forthFilter);

            compoundRowFilter = RowFilter.orFilter(filters);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(compoundRowFilter);
    }

以下是示例

picture example here