Filtering the rows of a JTable based on JCombobox selection
Hi,,,
I write an application where I have a combobox contains the JTable first column data when i click on the item it should give me the rows, which contains the selected item as their first column data. I am enable to get the list like that, please, help me in this.........................................
Thanks In Advance,
Sun_sgrk
[361 byte] By [
sun_sgrka] at [2007-11-27 2:17:15]

# 1
You should:
a) retrieve the selected item
b) get the data in the JTable and select those rows which corresponds to the item selected. For this It's very useful a DefaultTableModel
In this way you will be able to have a Vector or a general set of rows so as to refresh your table
# 2
Hi Albert,,,,,,,,
My table model is DefaultTable Model only, I tried in that matter but I am not able to print the rows in the table. But when i am checking with the System screen its printing the rows data what ever i want. Please, tell me the other way.......
Regards,
Sun_sgrk
# 3
If you are using, or can switch to JDK 1.6, you can use a RowFilter (javax.swing.RowFilter).
If you check out the JDocs for this, there are some examples on how to implement RowFilter:
http://java.sun.com/javase/6/docs/api/javax/swing/RowFilter.html.
You should also find some helpful info here
http://java.sun.com/developer/technicalArticles/javase/6_desktop_features_2/index.html.
# 4
Hi..........I am not using JDK 1.6, our application is strict to JDK1.5 thats why, please, any one tell me in this how can i get the concept.Regards,Sun_sgrk
# 5
may you post your code? in this way we can see what is wrong..
# 6
Hi Albert,
I write the code like this.............., please, check it, if I am following any where wrong let me know.......
JComboBox filterCb=(JComboBox)evt.getSource();
if(filterCb==filterComboBox){
for(int i=0;i<wtOb.length;i++){
for(int j=0;j<4;j++){
if(wtOb[i][0].toString()==filterComboBox.getSelectedItem())
System.out.println(""+wtOb[i][j]);
}
}
}
This code I written in netbeans, when I am printing the values of wtOb[j] in console its getting the rows, suppose I am selecting the topic as GeneralTopic then its givining me the output as--
GeneralTopic
Dealer5
Player3
BlackJack2
GeneralTopic
Dealer7
Player6
BlackJack
but I am getting the trouble when i want to show these rows only in my table, please, give me any suggestion or any help to resolve it. I tried with table.setModel also but its not working.
Regards,
sun_sgrk>
# 7
try with a code like this. Memorize the table's header in a Vector called for example headerTable and call this method whenever you want to refresh the table
private void setTableModel (Vector rows, Vector header) {
DefaultTableModel model = new DefaultTableModel(rows, header) {
public boolean isCellEditable (int row, int col) {
return false;
}
};
table.setModel(model);
}
In this way the refresh should be right:
setTableModel(newRows,tableHeader)