简体   繁体   中英

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

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. My question is how can I remove the id column from the clone of TableModel ?

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

图片示例在这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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