简体   繁体   English

从jtable表模型克隆中删除数据列

[英]delete a column of data from a jtable tablemodel clone

I have a sample data populate from database to jtable using rs2xml.jar and I also added a jTextField to filter the jTable by referring this link ). 我有一个使用rs2xml.jar从数据库填充到jtable的样本数据,并且还添加了一个jTextField来通过引用此链接来过滤jTable。

My problem here is I populate the id column to jtable and I use it identify the row and cells data when user edit it. 我的问题是我将id列填充到jtable中,并在用户编辑它时使用它标识行和单元格数据。 My question is how can I remove the id column from the clone of TableModel ? 我的问题是如何从TableModel的克隆中删除id列?

Code is below: 代码如下:

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

这是我的应用程序的图像

First create a textfield then use the code below: 首先创建一个文本字段,然后使用以下代码:

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

Below is the example 以下是示例

图片示例在这里

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

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