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: Swing JTable sorting

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Swing JTable sorting

    excuse me, how about Selection sorting? I mostly use this kind of sorting in my small programs, and Im about to use this in sorting my JTable (an inventory system table), Im having problems with sorting it by using a swing method, so I want to apply some manual sorting using this Selection algorithm,


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Swing JTable sorting

    Have you tried implementing the Comparator/Comparable interface (not sure which it is, Sun's tutorial is quite vague, if all else fails implement both interfaces)?

    Here's Sun's tutorial for using JTable: Link. Just scroll down to the section about Sorting and Filtering.

    If you want to implement selection sort, see Wikipedia: Selection sort

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Swing JTable sorting

    oh world thanks for moving this in another thread, sorry, never thought of that,
    anyway here a sample code of my database that doesnt sort the item quantity

    public class NewClass extends JFrame {
     
        private JPanel panel;
     
        private JScrollPane scroll;
     
        private JTable inventoryTable;
        public NewClass() {
     
            initComponents();
        }
     
        private void initComponents() {
     
            final String[] columnNames = {"Item Code", "Item Quantity", "Item Desciption", "Item Cost",
                                          "Cost Extension", "Retail", "Retail Extension", "Expiration Code"};
     
                 Object[][] data = {{"1000008", new Integer(32), "TOBI MIX NUTS 45g", new Double(18.52), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000013", new Integer(45), "TOBI MIX NUTS 45g", new Double(30.00), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000002", new Integer(15), "TOBI MIX NUTS 45g", new Double(39.59), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)} ,
                               {"1000015", new Integer(323), "TOBI MIX NUTS 45g", new Double(31.35), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000018", new Integer(64), "TOBI MIX NUTS 45g", new Double(26.36), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000005", new Integer(123), "TOBI MIX NUTS 45g", new Double(45.41), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000021", new Integer(154), "TOBI MIX NUTS 45g", new Double(8.54), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000010", new Integer(235), "TOBI MIX NUTS 45g", new Double(5.08), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000007", new Integer(346), "TOBI MIX NUTS 45g", new Double(7.66), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000001", new Integer(234), "TOBI MIX NUTS 45g", new Double(6.19), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000019", new Integer(98), "TOBI MIX NUTS 45g", new Double(5.43), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000014", new Integer(65), "TOBI MIX NUTS 45g", new Double(37.35), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000017", new Integer(101), "TOBI MIX NUTS 45g", new Double(12.86), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000022", new Integer(56), "TOBI MIX NUTS 45g", new Double(12.86), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000004", new Integer(22), "TOBI MIX NUTS 45g", new Double(12.86), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000020", new Integer(10), "TOBI MIX NUTS 45g", new Double(12.86), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000012", new Integer(36), "TOBI MIX NUTS 45g", new Double(20.39), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000006", new Integer(98), "TOBI MIX NUTS 45g", new Double(20.39), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000009", new Integer(195), "TOBI MIX NUTS 45g", new Double(22.01), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000016", new Integer(234), "TOBI MIX NUTS 45g", new Double(22.01), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000011", new Integer(89), "TOBI MIX NUTS 45g", new Double(22.01), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000003", new Integer(40), "TOBI MIX NUTS 45g", new Double(20.39), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)}};
     
                 inventoryTable = new JTable(data, columnNames);
                 inventoryTable.setGridColor(Color.BLACK);
                 inventoryTable.setFont(new Font("Arial", Font.BOLD, 11));
                 inventoryTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
                 inventoryTable.setAutoCreateRowSorter(true);
                 inventoryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                 inventoryTable.getColumnModel().getColumn(0).setPreferredWidth(150);
                 inventoryTable.getColumnModel().getColumn(1).setPreferredWidth(150);
                 inventoryTable.getColumnModel().getColumn(2).setPreferredWidth(360);
                 inventoryTable.getColumnModel().getColumn(3).setPreferredWidth(140);
                 inventoryTable.getColumnModel().getColumn(4).setPreferredWidth(140);
                 inventoryTable.getColumnModel().getColumn(5).setPreferredWidth(140);
                 inventoryTable.getColumnModel().getColumn(6).setPreferredWidth(150);
                 inventoryTable.getColumnModel().getColumn(7).setPreferredWidth(160);
     
                 panel = new JPanel();
                 scroll = new JScrollPane(inventoryTable);
                 panel.setLayout(null);
     
                 scroll.setBounds(0, 300, 1395, 444);
                 panel.add(scroll);
     
                 getContentPane().add(panel);
     
                 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                 setSize(1400, 800);
                 setLocationRelativeTo(null);
                 setResizable(false);
                 setVisible(true);
        }
     
        public static void main(String[] args) {
     
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
     
                    new NewClass();
                }
            });
        }
    }

    Ill read those links that you gave me, thank you for that, ill just post the sample code here for some viewing and a little help..

Similar Threads

  1. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  2. JTable in JScrollPane
    By alwayslearner in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 29th, 2010, 11:42 AM
  3. Java Swing Tables ( JTable Models ) to connect to Database
    By javaprogrammer in forum JDBC & Databases
    Replies: 0
    Last Post: January 26th, 2010, 04:13 PM
  4. Replies: 0
    Last Post: January 26th, 2010, 04:10 PM
  5. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM