Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Get data filter in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Get data filter in JTable" Watch "Get data filter in JTable" New topic
Author

Get data filter in JTable

tok chan
Greenhorn

Joined: Jan 19, 2011
Posts: 2
Hi, i'm beginner in JAVA, need some help.
I have filtered my data in JTable, then i want to export data filtered to excel.
How to get data that i have filtered ?

my code filter JTable is like below :
String header[] = { "Name", "Country" };
Object[][] data = { { "Ryu", "Japan" }, { "Joong Ki", "Korea" },
{ "Hideaki", "Japan" }, { "Keanu", "English" } ,
{ "Watase Yuu", "Japan"} , {"Hyun Bin","Korea"}
};

jTable1.setModel(new DefaultTableModel(data, header) {
boolean[] canEdit = new boolean[]{false, false
};
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
});

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("Korea", 1));
rf = RowFilter.andFilter(filters);

sorter = new TableRowSorter<TableModel>(jTable1.getModel());
sorter.setRowFilter(rf);
jTable1.setRowSorter(sorter);

Please help me...

Thanks,
Tokchan

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Welcome to the Ranch!

Use the JTable to get the values, not the TableModel. JTable.getValueAt etc use the row filter, row sorter et all. In other words, it will use exactly what is shown on the screen.

And please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
tok chan
Greenhorn

Joined: Jan 19, 2011
Posts: 2
Rob Spoor wrote:Welcome to the Ranch!

Use the JTable to get the values, not the TableModel. JTable.getValueAt etc use the row filter, row sorter et all. In other words, it will use exactly what is shown on the screen.

And please UseCodeTags next time.


Yes i know, but how to get data in that column ? because i want to export the data to file.
For example :
Line Name City
----------------------------------
1 A America
2 B Singapore
3 C Singapore
4 D Korea
5 E Japan

Then i filter City : Singapore

The Result is
Line Name City
----------------------------------
2 B Singapore
3 C Singapore


When i try to get with getvalueat, the data in table is not read filter data, but all data.
So i'm confused, how to get just only data filtered ?

Thanks,
tokchan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get data filter in JTable
 
Similar Threads
Problem Updating Content Of JTable
RowFilter dont show rows in JTable
JTable RowFilter by row number
JTable rowFilter - is case insensitive possible?
JTable adding a RowFilter can no longer add to the table get invalid range?