Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JTable sorter example

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JTable sorter example

    I found an example of using DefaultTableModel to sort by columns. I understand all except:

            model = new DefaultTableModel(data, colnames)
            {
                public Class GetColumnClass (int column) {
                    if(column >= 0 && column <= getColumnCount())
                        return getValueAt(0, column).getClass();
                    else
                        return Object.class;
                }
            };

    I'm assuming GetColumnClass is called by one of the constructors, or at least it ensures that there is method defined for it. If I comment out that section, I can still generate a table, and if I click on the column names I can sort the data. Is it necessary to define that method?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JTable sorter example

    I have not seen that method. If the code works without it, you can probably leave it out.

    Did the example use it? What for?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JTable sorter example

    My Googling for JTable tablerowsorter came up with this one. It doesn't really explain what the code fragment in question does, other than it says it sorts it and follows the column class. Java Tutorial - Sort a JTable with TableRowSorter and follow column class in Java.

    I'm thinking since it seems to sort without it, providing this overriding method has less to do with the actual sorting, and merely provides a method that can be used if you need to learn the class of the column.

Similar Threads

  1. Replies: 0
    Last Post: October 4th, 2017, 12:24 PM
  2. Replies: 1
    Last Post: February 5th, 2014, 09:22 AM
  3. Bubble Sorter and 2d Array
    By nwegman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 25th, 2013, 02:07 PM
  4. Sorter Project
    By Justin.ITPRO in forum Object Oriented Programming
    Replies: 1
    Last Post: May 22nd, 2012, 06:09 AM