Author Archives: David Casleton

Keep your dataTable clean with a custom popup

The basic idea is to output some data to a user in a table and allow them to take an action on each row individually. A fairly straightforward solution is to create a separate page to link to, passing the necessary row information along. If the action is simple enough, like a single checkbox, you could just embed the necessary component(s) in each row of the table. Too many components, however, can bloat the table and make the UI cumbersome to the user. Instead we can create a popup window to overlay our page, containing whatever components are needed, and activate it by a link embedded in our table. Passing the row information is a little trickier, but the result is a cleaner interface and a better user experience.
Continue reading

Sorting your Beans

Need to sort a list of custom objects?   Instead of coding up a custom implementation of your favorite sorting algorithm, you can simply use the tools provided in the Collections class (Collections framework) paired with the BeanComparator class (Apache project).

Simple example: you have a List of Person objects and you want to sort via the lastName.

List<Person> people = ... ;
Collections.sort(people, new BeanComparator("lastName"));

Sorting complete!  And good news, the sort method guarantees n log(n) performance through a special implementation of merge sort.

Is your custom object more complicated?  Perhaps you encapsulated all the name information (first, middle, last) in another object within Person, called Name.

Collections.sort(people, new BeanComparator("name.lastName"));

Want the list sorted in descending order?

Collections.sort(people, new BeanComparator("name.lastName"));

There are some other great utilities in there including methods to find the min and max based on a given field, as above.  You could even search for a single element using their binary search implementation, but beware: your list should already be sorted!