I require a Sortable JTable. Please Help.

C

C-man

Hey, I am basically looking for the source code for a generic JTable that
implements sorting by handling a mouse click on the Table headers. Does
anybody know of a good java class that does that? Thanks
 
J

Jeffrey D. Smith

C-man said:
Hey, I am basically looking for the source code for a generic JTable that
implements sorting by handling a mouse click on the Table headers. Does
anybody know of a good java class that does that? Thanks

It is very simple. You want something like:

public class MyTable extends JTable
{
/** The number of clicks to initiate a column sort action. */
public static final int SortClicks = 1;

/**
* Sort the specified column. Sort the data model
* for the specified column index.
*
* @param theColumn The origin-0 column index.
*/
public abstract void sortColumn(int theColumn);

/**
* The MouseListener listens for mouse clicks on the
* JTableHeader for sorting columns.
*/
protected class ColumnClickListener
extends MouseAdapter
{
/**
* Receives control when the underlying visual component
* detects a mouse click (press and release) over it.
*/
public void mouseClicked(final MouseEvent mouseEvent)
{
final TableColumnModel columnModel;
final int xClick;
final int xView;
final int xModel;

columnModel = getColumnModel();
xClick = mouseEvent.getX(); // horizontal pixel
xView = columnModel.getColumnIndexAtX(xClick); // view index
xModel = convertColumnIndexToModel(xView); // model index

// if the user clicked on a valid column header,
// then sort all rows by that column.
if((0 <= xModel) && (SortClicks == mouseEvent.getClickCount()))
{
sortColumn(xModel);
}
}
}

// instance initializer: create and register the column header mouse
listener
{
final MouseListener mouseListener;
final JTableHeader jTableHeader;

mouseListener = new ColumnClickListener();
jTableHeader = getTableHeader();
jTableHeader.addMouseListener(mouseListener);
}

// everything else you need goes here, including constructors...
//...application specific stuff...
}
 
R

Roedy Green

Hey, I am basically looking for the source code for a generic JTable that
implements sorting by handling a mouse click on the Table headers. Does
anybody know of a good java class that does that? Thanks

It is not easy to write that in a generic way. New data can appear at
any time to upset the apple cart. I have written them as part of
other apps.

If you want I could hammer out a spec for something with you and give
you a price quotation to write it.
 
T

Tim Ward

C-man said:
Hey, I am basically looking for the source code for a generic JTable that
implements sorting by handling a mouse click on the Table headers. Does
anybody know of a good java class that does that? Thanks

It's actually non-trivial.

All the free examples I found out there, including Sun's, failed to work
properly once you start asking

(a) that selections in the table work properly
(b) that when adding rows, deleting rows and editing existing data the rows
stay sorted
(c) that sorting is done other than on the natural ordering of the cell
values (eg case insensitive sorting of strings, priority order sorting of
coloured blobs).

So I ended up having to write one that did all that.

And my client paid me to do it and it belongs to my client so I can't post
the code. Sorry.
 
D

Daniel Dyer

Hey, I am basically looking for the source code for a generic JTable that
implements sorting by handling a mouse click on the Table headers. Does
anybody know of a good java class that does that? Thanks

I have written a generic solution that will work in most situations (as
long as the values in the sortable columns implement Comparable and each
row is represented in the model by a distinct object). It basically
consists of a custom table header component to enable the sorting, an
extension to the TableModel interface to add a few methods to facilitate
sorting (along with a corresponding extension of AbstractTableModel for
convenience) and a comparator for comparing rows in a table (where each
row is a distinct object), based on the criteria provided by the header
component. Then all that is necessary to get it working is to extend the
AbstractSortableTableModel class and fill in the gaps. Sub-classing
JTable isunnecessary.

Do you want such a component for commercial use?


Dan.
 
C

C-man

No, I don't want to use this for commercial use. I just thought someone
would have a useful chunk of code for free. I thought it would be something
so simple that they would just hand over. But I suppose I will just put in
the time and finish it myself. Thanks anyway
 
D

Daniel Dyer

No, I don't want to use this for commercial use. I just thought someone
would have a useful chunk of code for free. I thought it would be
something
so simple that they would just hand over. But I suppose I will just put
in
the time and finish it myself. Thanks anyway

You may have misunderstood me. I'd be quite happy to provide you with
this code for non-commercial use (in fact I am in the process of putting
together a library of useful GUI components that I intend to make
available under an open source licence, of which the sortable table is
one), I just didn't want to be doing somebody's job for them by giving
away code for use in a commercial product.

If you are still interested send me an e-mail (do the obvious to the
e-mail address to reply).

Dan.
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

2003 23:05:53 GMT, "C-man"

| Hey, I am basically looking for the source code for a generic JTable that
| implements sorting by handling a mouse click on the Table headers. Does
| anybody know of a good java class that does that? Thanks

"Sorting the table"
<http://www.codeguru.com/java/articles/219.shtml>

<davidp />

- --
David Postill

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.2 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA/AwUBP4X/Inxp7q1nhFwUEQJUTgCcDxGkYQXVitrXMrz6oM2aFeGPtAcAnRGX
RCeKH1Z/ZofKnF/L7nTEYvyO
=JjGS
-----END PGP SIGNATURE-----
 
R

Roedy Green

FWIW I hardly get any spam to this legitimate address after over a year of
posting it it clear text on usenet. :)

I started getting spam almost immediately I switched to a new email
address. It is not posted as text anywhere. It is posted however as a
GIF on my website. All those Nigerians wanting to give me millions
somehow noticed my new address almost immediately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top