• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to sort JTable data on Year between a range of years

 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

i have a JTable with data that contains a column with Years.
I want to get the user to be able to sort the data between a range of two years.

Example:

a user sets te filter so he/she can view all the data between the years 1990 and 2000.

I am a beginner in Java and don't know how to code such a filter.

I gues it will be something with an if/Else statement or a loop?

Can anyone help me?

Thanks

Regards
Wim
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the javax.swing.RowFilter class.
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Check out the javax.swing.RowFilter class.


Ok thanks, i try it out.

Wim
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wim Van Geyt wrote:
I am a beginner in Java and don't know how to code such a filter.

Welcome to the Ranch.

The Swing API docs almost always have a how to use xxx section. If you check out the JTable API, you will notice this section points to http://docs.oracle.com/javase/tutorial/uiswing/components/table.html where you can find lots of useful information as well as code snippets
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i code some stuff for this solution. But i am still stuck with something...

I have used a for loop to go through the list from Year1 to Year 2.

Then i use the RowSorter and RowFilter to get the data between these two years.

But I have two fields fYear1 and fYear2 as textfields. And the RowFilter excepts just one String parameter.

I don't know what to put as parameter in the RowFilter/RegexFilter.

Any help?

This is my code:
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the RowFilter excepts just one String parameter.

- not true at all, have to read / re_read official Oracle tutorial, API

- you have look at

1. Regex(String), Number, Date

2. RowFilter.ENTRY in public boolean include(RowFilter.Entry entry) {

3. AND, OR, NOR for range and together with RowFilter.ComparisonType

- use some of JCalendar or JSpinner

- code, should be (put it to util.List as parameter and use this List for standard RowFilter),

RowFilter<TableModel, Integer> low = RowFilter.dateFilter(RowFilter.ComparisonType.AFTER, d1, 3);
RowFilter<TableModel, Integer> high = RowFilter.dateFilter(RowFilter.ComparisonType.BEFORE, d2, 3);
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

i have solved this issue.

The year is an Integer in the db and the program.
So i did an if with the integers that says year >= year1 && year <= year2.

That did the tric!

Thanks for all the answers!

Kind regards
Wim
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

i thought i solved it but it works not so fine as i hoped

So i tried it in another way. But i am stuck here too!

I don't get syntax errors but i seems not to fitler between the two years i type in the textfields.

So year1 and year2 are the names of the two textfields that i use for the range.

My code:
First the filter methods i use:


Second the addActionListener for the button


Maybe i am doing something very wrong?
Can anyone help me?
Thanks
Wim
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere you need a TableRowSorter. And somewhere you need to be passing it a RowFilter object via its setRowFilter() method. I don't see any code like that.
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

ok i will use a TableRowSorter.

I used it this way: (view code). But it seems not to work.



Regards
Wim
 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have almost the solution, I think...

This code does the filtering. But it only sets the last result in the JTable. So if you give in year1 (1963) and in year2 (1965) he will loop through every year from 1963 to 1965.
But he will only put the data of 1965 in the table.

Now i have to find something in the RowFilter and regexFilter that will do the trick. The trick is: putting all data from 1963 to 1965 in the table.
Where can i find this functions of the Row- and regexFilter?


 
Wim Van Geyt
Greenhorn
Posts: 22
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i have been searching for answers and i found out that i have to use the Iclude(entry...) stuff for the range filter to work.

I found everywhere the same code with the same unclear explanation.

Can anyone explain clearly to me what this statement means?



Questions:
1. Is this statement used for filtering a range? E.g. between two years?
2. What's the entry for? Is it eg the year1 and or the year2 you get from the textfield?
3 how do i use this include and entry stuff?

Thanks a lot.
Wim
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my table filter code:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!