Sort a jtable within code

C

clusardi2k

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,
 
A

Arne Vajhøj

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
 
L

Lew

John said:

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/tutor...DemoProject/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?
 
J

John B. Matthews

Lew said:
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.
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.
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top