Results 1 to 5 of 5
- 05-16-2011, 10:34 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
How to filter a JTable ? confused,
Hi,
I just want to filter my JTable, which consists of rows from a database.
Many of you can say,"That is not advanced, very easy" but not et al.
I found an approach for filtering, and works fine but the problem is while sorter objects uses model, I have no model in my code. The code I have found is not making table from a database, but from objects[]
Here is the working code,
Object rows[][] = {
{"AMZN", "Amazon", 41.28},
{"EBAY", "eBay", 41.57},
};
Object columns[] = {"Symbol", "Name", "Price"};
TableModel model =
new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
JTable table = new JTable(model);
and sorter objects uses that model for row sorter
final TableRowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
--------------------------------------------
And here is the my code for making te table from a resultset
try {
//columns
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
//rows
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// make Table
// Jtable table = new JTable( rows, columnHeads );
By the way, I think it is interesting to use TableModel in this way, because on the offical java TableModel Interface, the example given like this
TableModel myData = new MyTableModel();
JTable table = new JTable(myData);
Any idea or approach, how can handle this , and filter my JTable which I make from an access database ?
- 05-16-2011, 11:03 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example of filtering.
- 05-17-2011, 03:10 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Please use code tags when posting codes.
I repost your code, notice the bold codeI found an approach for filtering, and works fine but the problem is while sorter objects uses model, I have no model in my code. The code I have found is not making table from a database, but from objects[]
Java Code:Here is the working code, Object rows[][] = { {"AMZN", "Amazon", 41.28}, {"EBAY", "eBay", 41.57}, }; Object columns[] = {"Symbol", "Name", "Price"}; [b] //this is the model of your JTable TableModel model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } };[/b] JTable table = new JTable(model);And you have already created model for your JTable.and sorter objects uses that model for row sorter
final TableRowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);Last edited by mine0926; 05-17-2011 at 03:13 AM.
- 05-17-2011, 03:02 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
I think something misudnerstood here. That one (first code block) is the example for filtering JTable from jaav site, and works fine on that JTable.
The second code block is my code fro JTable, and my JTable constructs from resultset and has not model.
I need a model in order to make filtering , but I do not know how to setup my Jtable with a model, that uses resultset for data , instead of objects..
- 05-17-2011, 04:29 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Of course it has a model. A JTable MUST have a model. The model is created automatically for you when you pass the "rows' Nd "columnHead" to the JTable constructor. You can access the TableModel at any time by using:The second code block is my code fro JTable, and my JTable constructs from resultset and has not model.
Java Code:TableModel model = table.getModel();
Similar Threads
-
Filter JTable using andFilter and orFilter
By mine0926 in forum New To JavaReplies: 3Last Post: 02-08-2011, 06:48 AM -
update jtable with filter
By simo_mon in forum AWT / SwingReplies: 7Last Post: 06-15-2010, 04:53 AM -
JTable with "Filter" like EXCEL
By atom86 in forum AWT / SwingReplies: 1Last Post: 10-14-2009, 10:18 AM -
Sort and filter in Jtable (java ver 1.5 )
By itaipee in forum AWT / SwingReplies: 3Last Post: 04-16-2009, 12:03 PM -
web content filter or internet filter
By sundarjothi in forum Advanced JavaReplies: 3Last Post: 05-15-2008, 11:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks