所以这是一个家伙如何做到的例子
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TestTableSortFilter extends JPanel {
private String[] columnNames
= {"Country", "Capital", "Population in Millions", "Democracy"};
private Object[][] data = {
{"USA", "Washington DC", 280, true},
{"Canada", "Ottawa", 32, true},
{"United Kingdom", "London", 60, true},
{"Germany", "Berlin", 83, true},
{"France", "Paris", 60, true},
{"Norway", "Oslo", 4.5, true},
{"India", "New Delhi", 1046, true}
};
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
private JTable jTable = new JTable(model);
private TableRowSorter<TableModel> rowSorter
= new TableRowSorter<>(jTable.getModel());
private JTextField jtfFilter = new JTextField();
private JButton jbtFilter = new JButton("Filter");
public TestTableSortFilter() {
jTable.setRowSorter(rowSorter);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Specify a word to match:"),
BorderLayout.WEST);
panel.add(jtfFilter, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(panel, BorderLayout.SOUTH);
add(new JScrollPane(jTable), BorderLayout.CENTER);
jtfFilter.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) {
String text = jtfFilter.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
@Override
public void removeUpdate(DocumentEvent e) {
String text = jtfFilter.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
@Override
public void changedUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame("Row Filter");
frame.add(new TestTableSortFilter());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
并且它工作正常,但问题是当我尝试通过点击此行获取值
((DefaultTableModel) jTableZ.getjTable1().getModel()).getValueAt(row, 36))
它没有返回此程序搜索的行,而是返回的行首先放在jTable中,没有搜索。让我尝试解释: 当我在jTable中有多行并且搜索将其缩小为1行时,我会得到旧jTable中的第一行,而不是行搜索它的行。如何解决这个问题?
我忘了写我的数据不像这里那样
private Vector<Vector<String>> data
答案 0 :(得分:0)
要搜索JTable,请尝试以下方法:
For(int row=0;row<C.ROWS; row++){
For(int col=0;col<C.COLS; col++){
if(table.getValueAt(row, col) == (Object) O){
System.out.print(Hello);
}
}
}
根据是否有所不同,您可以在此时使用JTable I对象的每个单元格中设置值。
答案 1 :(得分:0)
我已经解决了它而不是getSelectedRow我必须将其转换为rowIndexModel以获取所选行
private int row = jTable1().getSelectedRow();
private int rowModel=jTable1().convertRowIndexToModel(row);
它运行良好:)