从 jtable 表模型克隆中删除一列数据
delete a column of data from a jtable tablemodel clone
我使用 rs2xml.jar 将示例数据从数据库填充到 jtable
并且我还添加了一个 jTextField 以通过引用此 ).
来过滤 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;
}
}
}
}
首先创建一个文本框,然后使用下面的代码:
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);
}
下面是例子
我使用 rs2xml.jar 将示例数据从数据库填充到 jtable
并且我还添加了一个 jTextField 以通过引用此
我的问题是我将 id 列填充到 jtable 并在用户编辑时使用它来识别行和单元格数据。我的问题是如何从 TableModel
?
代码如下:
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;
}
}
}
}
首先创建一个文本框,然后使用下面的代码:
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);
}
下面是例子