Results 1 to 8 of 8
- 09-19-2009, 01:38 AM #1
Sorting JTable on more than just the values listed in the columns
I have a collection of objects that store (among other things) two integer values. I only want to display one of these values. The displayed value will dictate the sort order of the JTable.
However, I want to break ties with the other, undisplayed, integer value.
I've gone through the trouble of specifying my own Comparator for the TableRowSorter. However, this method doesn't have access to the row index into the table. It is only passed the values in the column that is being sorted, not their position in the column. Without having a reference back to the row index (which I use as an index to the collection of objects) I cannot get the secondary value, unless I also display this value in its own column and sort on Sort Keys.
I don't want to do that, because I feel it clutters up the look of the table.
For example, I have the two sets of numbers
Row Name | Displayed Value | Undisplayed Value
rowA | 7 | 3
rowB | 7 | 5
With this set of data, rowB should be listed above rowA. Any ideas on how to implement this.
I'm pretty new to Swing, so there could be something simple that I'm missing (hopefully).Last edited by Jeegen; 09-19-2009 at 01:53 AM. Reason: typo
- 09-21-2009, 06:18 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Instead of storing the integer data in two separate columns, you need to create an Object that contains both integer values and then store that Object in a single column in the TableModel. You will then need to write a custom renderer to display the first integer value.
- 09-21-2009, 07:45 PM #3
OK, I didn't know about that approach. I will dig into that, now.
Thank you for the help!
-J
- 09-23-2009, 01:10 AM #4
That worked!
All I had to do was create a small internal class (InitiativeStorage), which was held in my own Table Model (CharTableModel). This class has it's values initialized upon creation and accessor methods to read the data.
CharTableModel stores an array of these classes and returns the appropriate instance from the array in getValueAt().
For setValueAt(), I update just the single data field I want displayed. The other data shouldn't change.
For my custom renderer, I just extended DefaultTableCellRenderer and modified getTableCellRenderer to pull the data out of the interal class. This value is then passed to the original cell renderer...
Then, I just had to assign this renderer to the column...Java Code:public class InitiativeCellRenderer extends DefaultTableCellRenderer { public InitiativeCellRenderer() { super(); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // Is this the column I want to sort? if (column == CharTableModel.INIT_COLUMN) { // just display the initiative value int displayValue = ((CharTableModel.InitiativeStorage) value).getInitiative(); return (super.getTableCellRendererComponent(table, displayValue, isSelected, hasFocus, row, column)); } // in most cases, return the default table cell renderer return (super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)); } }
Now, my problem is that I cannot edit the cells in this column.Java Code:charTable.getColumnModel().getColumn(CharTableModel.INIT_COLUMN).setCellRenderer(new InitiativeCellRenderer());
Last edited by Jeegen; 09-23-2009 at 01:17 AM. Reason: typo
- 09-23-2009, 02:09 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Now you also need to create a custom editor. I would start by extending the DefaultCellEditor. You have the same basic logic except its a two step procedure. You need to get your custom Object and populate the editor the the value to be edited. Then when editing is finished you need to take the edited value and update your custom Object so it can be returned from the editor and then restored back to the model.
- 09-23-2009, 08:10 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
what is this, CharTableModel? is this your custom class? i could not find this in the API
- 09-23-2009, 05:29 PM #7
Yes, CharTableModel is my implementation of AbstractTableModel.
Sorry for the confusion. I try to make my posts as short as possible, which is a struggle for me. In the process, I guess I didn't explain things well enough.
- 09-23-2009, 07:28 PM #8
Similar Threads
-
Sorting JTable
By mm2236 in forum AWT / SwingReplies: 0Last Post: 04-09-2009, 04:37 PM -
How to sort a JTable for multiple columns?
By BLR in forum Advanced JavaReplies: 2Last Post: 03-16-2009, 10:41 AM -
Right Align columns in JTable
By Laura Warren in forum New To JavaReplies: 2Last Post: 12-18-2008, 09:01 PM -
sorting JTable
By mansi_3001 in forum Advanced JavaReplies: 3Last Post: 08-10-2007, 06:29 PM -
sort columns in jtable
By Alan in forum AWT / SwingReplies: 2Last Post: 05-14-2007, 05:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks