Take the 2-minute tour ×

i want the numbers to be displayed in this format..

1 2 3 4 5 ^
where if i press 5, then it should display from 5 to 10

5 6 7 8 9 10

till the max records are available. i just want to know a how to do this for displaying numbers.

share|improve this question
What is your user interface framework? If it's the console then it's not so simple to do portable non-blocking I/O under Java. –  trojanfoe Apr 2 '11 at 7:27
i have implemented using Spring MVC, where if i click on 1, it will display from 1 to 10 records. now the only problem is displaying numbers that are meaningfull. currently i have implemented showing numbers from 1 to 10 for 100 records. but if records increase, then the displaying numbers too will increase. so want to control this.. how do i do that? –  Raju Apr 2 '11 at 7:36
i just want to know the logic of how this can be done. or if anybody can show with simple example for this calculation? –  Raju Apr 2 '11 at 7:49
add comment

4 Answers

up vote 4 down vote accepted

Normally you want your database to do the heavy lifting for pagination and sorting. With MySQL for example you can get a page of sorted results by adding


to the end of your SQL query. If you use hibernate you can do this in a vendor-independent way using the criteria API:

(List<MyDomainClass>) Criteria criteria = getSession()     

To display a pagination navigation you also need to count the total number of results:

(Integer) Criteria criteria = getSession()     

You can add restrictions to both searches, for example when you want to filter on lastname you could add:

.add(Restrictions.eq("lastname", "Smith")

(This needs to be added to both the count query and the list query).

When you know the total number of results, the pagesize and the current page number you can do something like this:

// TODO: execute the count query here to determine totalResults
int totalPages = Math.ceil(totalResults / pageSize);
// show first page by default
int first = 0;
if (currentPage >= 0 && currentPage < totalPages) {
    first = currentPage * pageSize;    
// the number of items on the page might be less than the page size
int count = Math.min(pageSize, totalResults - first);        
// TODO: execute the list query here to get a page of data

How you display the navigation is up to you. Some frameworks (like my favorite, Wicket) have standard components for that. Otherwise you will have to think of a way to handle huge amounts of pages. A common approach is to show a range of say 10 page numbers and forward/backward jump to start/jump to end links.

Hope this helps.

share|improve this answer
add comment

We cannot give the specific/exact answer you expected. The thing here is we can give you some advice for some logic ways to accomplish your request.

First thing is, read some documents for creating a well designed structure of your pagination. Here are some links that may help you in building the design of your pagination.

For my personal experience in designing my pagination and a large data will retrieve from a database. I'll use a LIMIT in my sql query to fasten retrieving the results. And I'll do things as follows.

  • First I do have a Model a class/object that will hold the pages that will display in my presentation layer.
  • The processing of query will process in a DAO/my business layer.
  • Now in my Services/Logic layer will process the assigning of values to my model. This where also the forwarding and requesting of data will be handle.
  • And in my presentation layer which the results of the query and the parameter for every links in pagination will display.

I know my explanation is not clear but I hope you get some point on it.

And now you could ask what's the relevant to your question. It is the process of how your presentation communication with your business layer. What I mean is the flow on how will you get the result you needed if some page number was call. Seems not really clear for that I'll make pseudo-codes that more related to you're question.

Since you don't have a ( First | Prev | page no here.... | Next | Last ) on your pagination. I'll make it short as possible.

First loading of the page with default pages. Assuming page data is from database and with the use of LIMIT

  1. DAO/Business - SELECT pagesContent/link FROM someTables LIMIT 0,5;..
  2. Services/Logic - Put links for page 1 and so on... and put it to pagination Model..
  3. Presentaton/Display - Display now the pages that stored in pagination Model.. Now in page5 or the last page you get the query you will pass another parameter that will change the starting limit of the query and you can pass also the nth page..

The logic here is, from your pagination display you will need to pass a value that your logic layer will understand what will be the next pages to be display. Another way is you can set a condition in you logic layer how to count the pages if it is not in the range of 1 - 4 or other thing you need to validate.

I hope this can be a help.

share|improve this answer
thank you for your reply. i have finished the part of access the and displaying the records. the only thing missing is the numbers that needs to be displayed. i tried this. if((currentPageNumber % 5 == 0) { firstPageNumber = currentPageNumber; endPageNumber = currentPageNumber + 5 ; if(endPageNumber >= totalRecords) endPageNumber = totalPages; } –  Raju Apr 2 '11 at 9:51
that didnt work, if i click 6, it ll display from 1 to 5 again. need some logic to retain from 5 to 10 even if i click 6 or 7 or so.. –  Raju Apr 2 '11 at 9:57
Your close to it.. Why it didn't work for 6.. maybe because you don't set the starting page to be displayed. Like I said there must be another parameter that can detect what will be the starting page. And for example a query like Select * FROM table LIMIT start-Page,nth-Page if you are using a query. But this can also apply to your process. Just play and analyze on your code and you will get it :) –  ace Apr 2 '11 at 10:11
hmm thank you for all advice. :D –  Raju Apr 2 '11 at 10:22
I think this was a great answer 1+ –  sfrj Apr 2 '11 at 11:55
show 1 more comment

Last week i discovered primefaces, it has lots of cool tools and gadgets, here have a look http://www.primefaces.org/showcase/ui/datatableHome.jsf if what you do is desktop apps you can have a look at javaFX http://javafx.com/

share|improve this answer
Well you can also control that yourself with no need of a gadget. I think an idea could be making a custom component of your own that as input parameter takes the total number of records and also the selected paginator value. Then you would design an algorithm that will divide the ammount of records by the selected paginator choice. The result will be the number of pages. You will need exception handling to cover all options. The pages numbers can be represented as an array. –  sfrj Apr 3 '11 at 16:53
add comment

If you have a List of records then you can do pagination using subList method of List.

List.subList(fromIndex, endIndex);

returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive

share|improve this answer
add comment

Your Answer


