Results 1 to 8 of 8
  1. #1
    Jeegen's Avatar
    Jeegen is offline Member
    Join Date
    Sep 2009
    Location
    Cali
    Posts
    5
    Rep Power
    0

    Question 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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    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.

  3. #3
    Jeegen's Avatar
    Jeegen is offline Member
    Join Date
    Sep 2009
    Location
    Cali
    Posts
    5
    Rep Power
    0

    Default

    OK, I didn't know about that approach. I will dig into that, now.

    Thank you for the help!

    -J

  4. #4
    Jeegen's Avatar
    Jeegen is offline Member
    Join Date
    Sep 2009
    Location
    Cali
    Posts
    5
    Rep Power
    0

    Default

    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...

    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));
      }
    }
    Then, I just had to assign this renderer to the column...

    Java Code:
    charTable.getColumnModel().getColumn(CharTableModel.INIT_COLUMN).setCellRenderer(new InitiativeCellRenderer());
    Now, my problem is that I cannot edit the cells in this column.
    Last edited by Jeegen; 09-23-2009 at 01:17 AM. Reason: typo

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    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.

  6. #6
    chitgoks is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default

    what is this, CharTableModel? is this your custom class? i could not find this in the API

  7. #7
    Jeegen's Avatar
    Jeegen is offline Member
    Join Date
    Sep 2009
    Location
    Cali
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    Jeegen's Avatar
    Jeegen is offline Member
    Join Date
    Sep 2009
    Location
    Cali
    Posts
    5
    Rep Power
    0

    Default another helpful reply :)

    Quote Originally Posted by camickr View Post
    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.

    I'll dig into this in the next couple of days. I really appreciate your help with this, camickr.

Similar Threads

  1. Sorting JTable
    By mm2236 in forum AWT / Swing
    Replies: 0
    Last Post: 04-09-2009, 04:37 PM
  2. How to sort a JTable for multiple columns?
    By BLR in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2009, 10:41 AM
  3. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 09:01 PM
  4. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:29 PM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 05:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •