Results 1 to 5 of 5

Thread: Sort the elements of a table in java

  1. #1
    Join Date
    Nov 2009
    Posts
    712

    Sort the elements of a table in java

    Hello, I am creating a project in java and while working on it, I want to sort the data from my database table. If anyone from you having idea about the sorting from the data of the table then please help me to sort the data. I have tried to sort it with the some logic by using the code, but it is not working perfectly all of the time. So, if you are having knowledge about the sorting technique technique in java then please provide help to me.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Sort the elements of a table in java

    Table sorting and filtering is managed by a sorter object. The easiest way to provide a sorter object is to set autoCreateRowSorter bound property to true:
    Code:
        JTable table = new JTable();
        table.setAutoCreateRowSorter(true);
    This action defines a row sorter that is an instance of javax.swing.table.TableRowSorter. This provides a table that does a simple locale-specific sort when the user clicks on a column header.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Sort the elements of a table in java

    Hello, If you want to sort the table in a swing application then you need to make use of Sorting tables in your application. If you are using the JDK 1.6 then it is having an additional feature of the sorting and filtering mechanism to JTable which makes use of the RowSorter. For solving the sorting problem Sun defines two classes as below:
    • TableMap
    • TableSorter

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Sort the elements of a table in java

    I have the code below which I have created in JDK 6 for sorting the elements of a table in java:
    Code:
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    public class Sorting {
      public static void main(String[] args) {
        Object[][] obj = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
        String cName[] = { "Item", "Value" };
        TableModel tablemodel = new DefaultTableModel(obj, cName) {
          public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
          }
        };
        JTable table = new JTable(tablemodel);
    
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tablemodel);
        table.setRowSorter(sorter);
    
        JScrollPane spane = new JScrollPane(table);
        JFrame frm = new JFrame("Sorting Table");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.add(spane);
        frm.setSize(300, 200);
        frm.setVisible(true);
      }
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Sort the elements of a table in java

    It is quiet simple to sort the data from the table. You just need to add the following line of code in your program.
    Code:
    JTable testing = new JTable(model);
    RowSorter<TableModel> sorter =  new TableRowSorter<TableModel>(model);
    testing.setRowSorter(sorter);

Similar Threads

  1. How to sort multiple columns in Excel Pivot Table
    By Nicoloid in forum MS Office Support
    Replies: 2
    Last Post: 17-02-2012, 08:01 PM
  2. Automatic sort and update league table in Excel
    By Raju Chacha in forum Windows Software
    Replies: 3
    Last Post: 08-01-2012, 04:18 PM
  3. Sort a table for several classes
    By New ID in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:19 AM
  4. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  5. How to Sort elements of Collection
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,402,534.72622 seconds with 17 queries