为了在表上搜索内容,必须将原始内容存储在某个变量中。在下面的示例中,我将表模型的初始值存储在向量中。
下面有两种实现方式:一种是在文本字段上键入内容后立即进行搜索,另一种则仅在单击按钮后进行搜索。
对于第一个,您必须使用documentListener为您做事。
下面的函数将删除表值(如果找不到匹配项,则表为空),并在所有单元格中搜索字符串,如果找到匹配项,则会将该行添加到表中。
public void searchTableContents(String searchString) { DefaultTableModel currtableModel = (DefaultTableModel) table.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; } } }}
这是上面的完整代码
import java.util.Vector;import javax.swing.event.documentEvent;import javax.swing.event.documentListener;import javax.swing.table.DefaultTableModel;public class TableSearch extends javax.swing.Jframe {Vector originalTableModel;documentListener documentListener;public TableSearch() { initComponents(); setLocationRelativeTo(null); //backup of original values to check originalTableModel = (Vector) ((DefaultTableModel) table.getModel()).getDataVector().clone(); //add document listener to jtextfield to search contents as soon as something typed on it adddocumentListener();}private void adddocumentListener() { documentListener = new documentListener() { public void changedUpdate(documentEvent documentEvent) { search(); } public void insertUpdate(documentEvent documentEvent) { search(); } public void removeUpdate(documentEvent documentEvent) { search(); } private void search() { searchTableContents(jTextField1.getText()); } }; searchOnType.setSelected(true);}@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); table = new javax.swing.JTable(); jPanel1 = new javax.swing.JPanel(); searchonType = new javax.swing.JCheckBox(); jTextField1 = new javax.swing.JTextField(); searchButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); table.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {"masd", "asdad", "asdasda", "ert"}, {"gdfg", "name", "test", "dfg"}, {"rrrh", "dfg", "sdfsf", "sdf"}, {"ter", "retg", "wersd", "wer"} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane1.setViewportView(table); getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); searchOnType.setText("Search on Type"); searchOnType.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { searchonTypeItemStateChanged(evt); } }); searchButton.setText("Search"); searchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchOnType) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.baseLINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchButton) .addComponent(searchOnType)) .addContainerGap()) ); getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH); pack();}// </editor-fold>private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) { searchTableContents(jTextField1.getText());}private void searchonTypeItemStateChanged(java.awt.event.ItemEvent evt) { if (searchOnType.isSelected()) { jTextField1.getdocument().adddocumentListener(documentListener); } else { jTextField1.getdocument().adddocumentListener(null); }}public void searchTableContents(String searchString) { DefaultTableModel currtableModel = (DefaultTableModel) table.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; } } }}public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TableSearch().setVisible(true); } });}// Variables declaration - do not modify private javax.swing.JPanel jPanel1;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JTextField jTextField1;private javax.swing.JButton searchButton;private javax.swing.JCheckBox searchOnType;private javax.swing.JTable table;// End of variables declaration }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)