Results 1 to 10 of 10

Thread: JTable Sorting

  1. #1
    rival is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default JTable Sorting

    Hello, I am making a program for class, and it is a Swing GUI based program and you input a few different things and it puts it into a JTable. One value it puts in the table is a ratio that is calculated from two numbers the user inputs. I need to be able to sort the table by descending ratio values.
    Any help would be appreciated,
    Thanks

    JMenu

    Java Code:
    // Display All Animals Menu Method
        public void displayall ()
        {
            contentPane = getContentPane ();
            if (infoPane != null)
                contentPane.remove (infoPane);
    
            infoPane = new JPanel ();
            infoPane.setLayout (new BorderLayout ());
    
            L1 = new JLabel ("Peel Pet House Animals:", JLabel.CENTER);
            L1.setFont (new Font ("Arial", Font.BOLD, 24));
            infoPane.add (L1, BorderLayout.NORTH);
    
            L2 = new JLabel ("End of Page", JLabel.CENTER);
            infoPane.add (L2, BorderLayout.SOUTH);
    
            String [] columnNames = {"Breed", "Category", "Acquisition Price", "Selling Price", "Profit Ratio"};
            table = new JTable (animalData, columnNames);
            JScrollPane scrollPane = new JScrollPane(table);
            infoPane.add (scrollPane, BorderLayout.CENTER);
            contentPane.add (infoPane);
            validate ();
            
           
        }
    Website won't let me attach the .zip of the java file, so here's a link to it PeelPetHouse.java

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JTable Sorting

    Have you tried a TableRowSorter object?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rival is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JTable Sorting

    I am fairly new to swing, and java actually, and Im not too sure on how to input TableRowSorter into my program. I was also thinking that instead of sorting the table by the value, it will determine the highest to lowest integers and then place them in the table accordingly.

    Alternatively show where I would implement the TableRowSorter in my code.

    Thanks,
    Rival
    Last edited by rival; 03-20-2012 at 07:45 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JTable Sorting

    You haven't read the API documentation for that class because it has a few simple examples how to add it to your JTable ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rival is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JTable Sorting

    Might I also add Im using a course required Ready to Program 1.7.1 that is only using Java 1.4.2

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JTable Sorting

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JTable Sorting

    Quote Originally Posted by rival View Post
    Might I also add Im using a course required Ready to Program 1.7.1 that is only using Java 1.4.2
    Even if that course happens to be free, it's not worth the money you paid for it. You could go through the Oracle Java tutorials instead -- they are free.

    Would you want to learn driving in a car you have to hand-crank to start, with a manual choke and timing advancer that you have to keep adjusting according to speed and gradient?

    From Java 2 Platform, Standard Edition (J2SE) 1.4.2
    J2SE 1.4.2 is in its Java Technology End of Life (EOL) transition period. The EOL transition period began Dec, 11 2006 and will complete October 30th, 2008, when J2SE 1.4.2 will have reached its End of Service Life (EOSL)
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    rival is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JTable Sorting

    No it's a school course, which I need to graduate haha sorry for the misunderstanding.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JTable Sorting

    A course in software history?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    rival is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: JTable Sorting

    Quote Originally Posted by DarrylBurke View Post
    A course in software history?

    db
    I wish, but none the less, this is what I am given to paint with, so i must work with what I have.

Similar Threads

  1. Sorting in JTable
    By jobi in forum SWT / JFace
    Replies: 4
    Last Post: 10-04-2011, 11:24 AM
  2. Problem with sorting columns using Jtable
    By aspiring_programmer in forum AWT / Swing
    Replies: 0
    Last Post: 12-02-2010, 03:43 PM
  3. JTable sorting
    By mtyoung in forum Advanced Java
    Replies: 6
    Last Post: 11-10-2009, 06:59 AM
  4. Sorting JTable
    By mm2236 in forum AWT / Swing
    Replies: 0
    Last Post: 04-09-2009, 05:37 PM
  5. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 07:29 PM

Posting Permissions

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