tbboyett
asked on
JTable, how to sort when click header
Hello, I have various information stored data objects and pulled into JTables and I need to find a way so that when a user clicks on a particular header it will sort the data for that column and redisplay the data. Basically, I can do the sorting, but just need to know how to determine when they click a header and which one they click. Any suggestions are greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
when I compile it doesn't like the HaderListener, what package does this exist in?
header.addMouseListener(ne w HeaderListener(header,rend erer));
header.addMouseListener(ne
ASKER
Sorry about the above question, didn't realize in the example on:
http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/editableatomiccontrols/jtable/jtablesortrows.html
he had it in the main class
http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/editableatomiccontrols/jtable/jtablesortrows.html
he had it in the main class
ASKER
Some reason I'm getting:
java.lang.ArrayIndexOutOfB oundsExcep tion: 0 >= 0
at java.util.Vector.elementAt (Vector.ja va:432)
at javax.swing.table.DefaultT ableColumn Model.getC olumn(Defa ultTableCo lumnModel. java:280)
at line model.getColumn(i).setHead erRenderer (renderer) ;
have you guys ran into that before?
java.lang.ArrayIndexOutOfB
at java.util.Vector.elementAt
at javax.swing.table.DefaultT
at line model.getColumn(i).setHead
have you guys ran into that before?
Can you show us more context code of the line
>> model.getColumn(i).setHead erRenderer (renderer) ;
?
>> model.getColumn(i).setHead
?
Looks like you don't have columns yet, so getColumn(0) leads to that error
ASKER
Well I set up the table like this this
zipsJT.setFont(new Font("Arial", 0, 13));
zipsJT.setBackground(Globa lVarStore. tableBgCol or);
zipsJT.setForeground(Globa lVarStore. tableFontC olor);
zipsJT.setGridColor(Global VarStore.t ableGridCo lor);
zipsJT.setSelectionBackgro und(Global VarStore.t ableSelect BgColor);
zipsJT.setSelectionForegro und(Global VarStore.t ableSelect ionFgColor );
SortableTableModel dm = new SortableTableModel() {
public Class getColumnClass(int col) {
switch (col) {
case 0:
return String.class;
case 1:
return Date.class;
case 2:
return Integer.class;
case 3:
return Boolean.class;
default:
return Object.class;
}
}
public boolean isCellEditable(int row, int col) {
switch (col) {
case 1:
return false;
default:
return true;
}
}
public void setValueAt(Object obj, int row, int col) {
switch (col) {
case 2:
super.setValueAt(new Integer(obj.toString()), row, col);
return;
default:
super.setValueAt(obj, row, col);
return;
}
}
};
zipsJT.setModel(dm);
zipsJT.setAutoResizeMode(J Table.AUTO _RESIZE_OF F);
Then I i try to do this:
String[] headerStr = {"Distance","Direction","C ity","Zip" ,"Count"};
((SortableTableModel)zipsJ T.getModel ()).setCol umnIdentif iers(heade rStr);
int[] columnWidth = {60,60,150,60,100};
SortButtonRenderer renderer = new SortButtonRenderer();
TableColumnModel model = zipsJT.getColumnModel();
int n = headerStr.length;
for (int i = 0; i < n; i++) {
System.out.println("header " + i);
model.getColumn(i).setHead erRenderer (renderer) ;
model.getColumn(i).setPref erredWidth (columnWid th[i]);
}
JTableHeader header = zipsJT.getTableHeader();
header.addMouseListener(ne w HeaderListener(header, renderer));
I doesn't make it any further than that, any ideas?
zipsJT.setFont(new Font("Arial", 0, 13));
zipsJT.setBackground(Globa
zipsJT.setForeground(Globa
zipsJT.setGridColor(Global
zipsJT.setSelectionBackgro
zipsJT.setSelectionForegro
SortableTableModel dm = new SortableTableModel() {
public Class getColumnClass(int col) {
switch (col) {
case 0:
return String.class;
case 1:
return Date.class;
case 2:
return Integer.class;
case 3:
return Boolean.class;
default:
return Object.class;
}
}
public boolean isCellEditable(int row, int col) {
switch (col) {
case 1:
return false;
default:
return true;
}
}
public void setValueAt(Object obj, int row, int col) {
switch (col) {
case 2:
super.setValueAt(new Integer(obj.toString()), row, col);
return;
default:
super.setValueAt(obj, row, col);
return;
}
}
};
zipsJT.setModel(dm);
zipsJT.setAutoResizeMode(J
Then I i try to do this:
String[] headerStr = {"Distance","Direction","C
((SortableTableModel)zipsJ
int[] columnWidth = {60,60,150,60,100};
SortButtonRenderer renderer = new SortButtonRenderer();
TableColumnModel model = zipsJT.getColumnModel();
int n = headerStr.length;
for (int i = 0; i < n; i++) {
System.out.println("header
model.getColumn(i).setHead
model.getColumn(i).setPref
}
JTableHeader header = zipsJT.getTableHeader();
header.addMouseListener(ne
I doesn't make it any further than that, any ideas?
In the file SortableTableExample.java of the example you have first
>> JTable table = new JTable(dm); // <<< the table is filled here
then
>> TableColumnModel model = table.getColumnModel(); // <<< then you ask the column model
>> int n = headerStr.length;
>> for (int i=0;i<n;i++) {
>> model.getColumn(i).setHead erRenderer (renderer) ;
>> model.getColumn(i).setPref erredWidth (columnWid th[i]);
>> }
When you ask the column model of an empty table it won't contain columns yet.
>> JTable table = new JTable(dm); // <<< the table is filled here
then
>> TableColumnModel model = table.getColumnModel(); // <<< then you ask the column model
>> int n = headerStr.length;
>> for (int i=0;i<n;i++) {
>> model.getColumn(i).setHead
>> model.getColumn(i).setPref
>> }
When you ask the column model of an empty table it won't contain columns yet.
ASKER
please ignore the line
((SortableTableModel)zipsJ T.getModel ()).setCol umnIdentif iers(heade rStr);
I was just trying that to see it would help
((SortableTableModel)zipsJ
I was just trying that to see it would help
ASKER
what if i don't get the data till later on?
When you call
>> zipsJT.setModel(dm);
dm is still empty.
You have to fill up your data model by calling dm.setDataVector() before doing the rest
>> zipsJT.setModel(dm);
dm is still empty.
You have to fill up your data model by calling dm.setDataVector() before doing the rest
>> what if i don't get the data till later on?
delay
>> model.getColumn(i).setHead erRenderer (renderer) ;
>> model.getColumn(i).setPref erredWidth (columnWid th[i]);
until your model is filled up
delay
>> model.getColumn(i).setHead
>> model.getColumn(i).setPref
until your model is filled up
ASKER
So, can i not set the headers prior to setting the data?
Try filling the model with one row of null objects
String[] headerStr = {"Distance","Direction","C ity","Zip" ,"Count"};
dm.setDataVector( new Object[][] {{ null, null, null, null, null }}, headerStr);
zipsJT.setModel(dm);
followed by
int[] columnWidth = {60,60,150,60,100};
SortButtonRenderer renderer = new SortButtonRenderer();
TableColumnModel model = zipsJT.getColumnModel();
int n = headerStr.length;
for (int i = 0; i < n; i++) {
System.out.println("header " + i);
model.getColumn(i).setHead erRenderer (renderer) ;
model.getColumn(i).setPref erredWidth (columnWid th[i]);
}
dm.setDataVector( new Object[][] {{ null, null, null, null, null }}, headerStr);
zipsJT.setModel(dm);
followed by
int[] columnWidth = {60,60,150,60,100};
SortButtonRenderer renderer = new SortButtonRenderer();
TableColumnModel model = zipsJT.getColumnModel();
int n = headerStr.length;
for (int i = 0; i < n; i++) {
System.out.println("header
model.getColumn(i).setHead
model.getColumn(i).setPref
}
> Try filling the model with one row of null objects
unnecesary
unnecesary
thanx 4 axxepting
http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/editableatomiccontrols/jtable/jtablesortrows.html