Hi guys!
I have a problem with adding a row filter to the JTable component. Below is a code that I'm using:
public class FilterClass extends JPanel {
TableRowSorter rowSorter;
private DefaultTableModel tableModel;
public FilterClass() {
//super(new GridLayout(1,0));
initComponents();
rowSorter=new TableRowSorter();
tableModel = (DefaultTableModel) tableDetails.getModel();
rowSorter.setModel(tableModel);
tableDetails.setRowSorter(rowSorter);
}
private void initComponents() {
tableDetails = new javax.swing.JTable();
txtFirstName = new javax.swing.JTextField();
txtLastName = new javax.swing.JTextField();
//setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tableDetails.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"103", "AAA","BBB"},
{"104", "CCC","DDD"},
{"105", "EEE","FFF"},
},
new String [] {"ID",
"Name",
"Surename"}
));
tableDetails.setFillsViewportHeight(true);
jScrollPane1 = new javax.swing.JScrollPane(tableDetails);
add(jScrollPane1);
txtFirstName.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txFirstNameKeyReleased(evt);
}
});
txtLastName.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtLastNameKeyReleased(evt);
}
});
}
private void txFirstNameKeyReleased(java.awt.event.KeyEvent evt) {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^"+txtFirstName.getText(), 0));
}
private void txtLastNameKeyReleased(java.awt.event.KeyEvent evt) {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^" +txtLastName.getText(), 1));
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tableDetails;
private javax.swing.JTextField txtFirstName;
private javax.swing.JTextField txtLastName;
}
FilterClass fc = new FilterClass();
fc.setVisible(true);
fc.setBounds(10, 50, 620, 200);
panfortab.add(fc); //panfortab is the JPanel component
The problem is that the rows "txtFirstName" & "txtLastName" are not displayed. What could be wrong with this code?