Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sort a jtable within code

Reply
Thread Tools

Sort a jtable within code

 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-12-2012
How can I automatically sort a Jtable by a specific column within code, and still allow the user to (later) sort any column by clicking on the specific table header.

Thanks,
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      09-13-2012
On 9/12/2012 9:25 AM, wrote:
> How can I automatically sort a Jtable by a specific column within
> code, and still allow the user to (later) sort any column by clicking
> on the specific table header.


It does not work changing the data in the data model behind the
JTable?

Arne



 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      09-13-2012
In article <114c9b7c-7ed8-4f59-aa46->,
wrote:

> How can I automatically sort a JTable by a specific column within
> code, and still allow the user to (later) sort any column by clicking
> on the specific table header.


Have you looked at Sorting and Filtering?

<http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting>

You can restore your TableModel's intrinsic order as shown here:

<http://stackoverflow.com/a/5484298/230513>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-13-2012
John B. Matthews wrote:
> Have you looked at Sorting and Filtering?
> <http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting>


Oh, I like that site. Thank you.

> You can restore your TableModel's intrinsic order as shown here:
> <http://stackoverflow.com/a/5484298/230513>


I'd like to know your preference regarding the tutorial example

<http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSortDemoProject/src/components/TableSortDemo.java>

(and ain't the "tutorial/displayCode.html" cool, huh?)

Do you prefer

public class TableSortDemo extends JPanel {
public TableSortDemo() {
super(new GridLayout(1,0));

as they do it, or

public class TableSortDemo {
JPanel sorterPanel = new JPanel(new GridLayout(1,0));

etc.?

Also,

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

'Class<?>' or don't bother?

--
Lew
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-13-2012
I've solved this problem. Do you want the details.
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-13-2012
On Thursday, September 13, 2012 1:59:01 AM UTC-4, (unknown) wrote:
> I've solved this problem. Do you want the details.


http://www.java.net/node/698343

RowSorter sorter = getRowSorter();
List sortKeys = new ArrayList();
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);

Thanks,
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      09-14-2012
In article <f8e3b99f-0790-4d51-a25d->,
Lew <> wrote:

> John B. Matthews wrote:
>> Have you looked at Sorting and Filtering?
>> <http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting>

>
> Oh, I like that site. Thank you.


You are welcome. Many articles saw updates during the run-up to the
Java 7 roll-out. It's also an appealing example of integrating Java Web
Start.

> > You can restore your TableModel's intrinsic order as shown here:
> > <http://stackoverflow.com/a/5484298/230513>

>
> I'd like to know your preference regarding the tutorial example
>
> <http://docs.oracle.com/javase/tutori...tp://docs.orac
> le.com/javase/tutorial/uiswing/examples/components/TableSortDemoProject/src/co
> mponents/TableSortDemo.java>
>
> (and ain't the "tutorial/displayCode.html" cool, huh?)


Yes, although it took me a while to understand the "Download" link.

> Do you prefer
>
> public class TableSortDemo extends JPanel {
> public TableSortDemo() {
> super(new GridLayout(1,0));
>
> as they do it, or
>
> public class TableSortDemo {
> JPanel sorterPanel = new JPanel(new GridLayout(1,0));
>
> etc.?


Ideally, I try to follow Joshua Bloch, Effective Java, 2nd edition:

Item 16: Favor composition over inheritance

Practically, a JPanel is a convenient unit of containment and a handy
rendering surface, so I may honor item 16 more in the breach than the
observance. I can't fault other weaknesses in the tutorial too much;
the particular example cited predates annotations and generics, both
introduced in Java 5.

Item 17: Design and document for inheritance or else prohibit it.

The parent, JComponent, is a good example of the former. JPanel adds
very little: opacity, a default layout and a UI delegate.

> Also,
>
> public Class getColumnClass(int c) {
> return getValueAt(0, c).getClass();
> }
>
> 'Class<?>' or don't bother?


Citing the same source, Item 23, the unbounded wildcard type Class<?>
"is safe and the raw type isn't." Checking 22 Java 1.5+ examples, I've
(carelessly) used the raw type twice.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting a JTable inside a JTable cell? Tivo Escobar Java 1 04-12-2007 11:09 AM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
JTable within JTree On Ali Java 1 01-26-2007 02:44 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
Jtable sort example Ike Java 2 08-27-2003 09:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57