我正在学习如何使用 JTable,但我的搜索和删除功能都存在问题。
从我的代码中,如果我检查第 1 行和第 3 行并按删除按钮,它将完美删除。
如果我在搜索文本字段中输入 J 并按搜索按钮,将显示 John 和 Jane 现在,如果我检查两行并按删除按钮,然后再次按搜索按钮, 约翰和简将被删除包括凯特和安。
如果你们还是不明白我在说什么,请运行下面的代码。 按照我提到的步骤操作,您就会看到问题所在。 帮我。谢谢
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);
}
});
}
}
最佳答案
因为当您过滤表格时行号会发生变化。使用原始行号删除正确的行。
使用table.convertRowIndexToModel(row)
获取实际行号。
Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
示例代码:
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--;
}
}
}
});
关于java - JTable 搜索和删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340807/