Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18669 > unrolled thread

Sort a jtable within code

Started byclusardi2k@aol.com
First post2012-09-12 06:25 -0700
Last post2012-09-13 23:58 -0400
Articles 8 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Sort a jtable within code clusardi2k@aol.com - 2012-09-12 06:25 -0700
    Re: Sort a jtable within code Martin Gregorie <martin@address-in-sig.invalid> - 2012-09-12 19:39 +0000
    Re: Sort a jtable within code Arne Vajhøj <arne@vajhoej.dk> - 2012-09-12 20:48 -0400
    Re: Sort a jtable within code "John B. Matthews" <nospam@nospam.invalid> - 2012-09-12 21:12 -0400
      Re: Sort a jtable within code Lew <lewbloch@gmail.com> - 2012-09-12 22:51 -0700
        Re: Sort a jtable within code clusardi2k@aol.com - 2012-09-12 22:58 -0700
          Re: Sort a jtable within code clusardi2k@aol.com - 2012-09-13 07:53 -0700
        Re: Sort a jtable within code "John B. Matthews" <nospam@nospam.invalid> - 2012-09-13 23:58 -0400

#18669 — Sort a jtable within code

Fromclusardi2k@aol.com
Date2012-09-12 06:25 -0700
SubjectSort a jtable within code
Message-ID<114c9b7c-7ed8-4f59-aa46-c7480a677be7@googlegroups.com>
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,

[toc] | [next] | [standalone]


#18688

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2012-09-12 19:39 +0000
Message-ID<k2qodu$srk$1@localhost.localdomain>
In reply to#18669
On Wed, 12 Sep 2012 06:25:57 -0700, clusardi2k 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.
>
I've done it by initially loading a concrete extension of the 
AbstractTableModel from a sorted list of the rows that will display in 
the JTable. I handled resorting on a user-selected column by simply 
reloading the AbstractTableModel's extension with data sorted on the 
required column. The list was sorted by extracting it from a database 
using a SELECT....ORDERED BY SQL statement.
  

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#18714

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-09-12 20:48 -0400
Message-ID<50512d50$0$293$14726298@news.sunsite.dk>
In reply to#18669
On 9/12/2012 9:25 AM, clusardi2k@aol.com 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


[toc] | [prev] | [next] | [standalone]


#18715

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-09-12 21:12 -0400
Message-ID<nospam-03FC5A.21124012092012@news.aioe.org>
In reply to#18669
In article <114c9b7c-7ed8-4f59-aa46-c7480a677be7@googlegroups.com>,
 clusardi2k@aol.com 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>

[toc] | [prev] | [next] | [standalone]


#18725

FromLew <lewbloch@gmail.com>
Date2012-09-12 22:51 -0700
Message-ID<f8e3b99f-0790-4d51-a25d-fef63576a936@googlegroups.com>
In reply to#18715
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

[toc] | [prev] | [next] | [standalone]


#18726

Fromclusardi2k@aol.com
Date2012-09-12 22:58 -0700
Message-ID<9dbcfe66-1325-4de2-96d4-649331ceccba@googlegroups.com>
In reply to#18725
I've solved this problem. Do you want the details.

[toc] | [prev] | [next] | [standalone]


#18737

Fromclusardi2k@aol.com
Date2012-09-13 07:53 -0700
Message-ID<f02ef12b-6c41-4e29-aa81-4e92a52fc068@googlegroups.com>
In reply to#18726
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,

[toc] | [prev] | [next] | [standalone]


#18761

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-09-13 23:58 -0400
Message-ID<nospam-DFA37D.23580513092012@news.aioe.org>
In reply to#18725
In article <f8e3b99f-0790-4d51-a25d-fef63576a936@googlegroups.com>,
 Lew <lewbloch@gmail.com> 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/tutorial/displayCode.html?code=http://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>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web