Table Sorting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    #1

    Table Sorting

    I have a programming assignement that i have been working on for quite some time now. I need your guys' help. My assignement is to great a table with data in it. and it will have a combobox and 2 buttons to how it will be sorted( run the program and you'll see how its setup).The table will have a comboBox that includes the 4 headings. We have to use insertion and selection sorting. I have done some of the program, just need help on getting up and running good. There will also be two buttons (ā€œ0-9/A-Zā€ ) and (ā€œ9-0/Z-Aā€). The user will choose by which heading the table should be sorted and then press the button as to how it will be sorted. When that button is pressed the table will be sorted and appear correctly with the correct information for each person. As 2 of the headings are Strings and 2 are integers, you will need to make the sorting of the Last name and age a selection sort and the sorting of the First name and test score insertion sorts. Check out the code, thanks for the help guys.
    Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JTable;
    import javax.swing.JComboBox.*;
    
    
    
    	
    
    public class Main implements ActionListener
    {
        JPanel p1;
        Object[] test;
           JFrame f1;
          JButton b1,b2;
             JTable table;
            JComboBox combo;
           Container c1;
          GP g1;
         JScrollPane scrollpane;
           public Main()
        {
               String[] columnNames = {"First Name","Last Name","Age","Test Scores"};
                 String[] combodata = {"First Name","Last Name","Age","Test Scores"};               
           // Smith       Dave        12      75
           // Johnson     Bill        14      72
           // Anderson    Sara        11      92
           // Hoyez       Kara        16      83
           // Cruz        Kyle        13      84
           String[] test = {"","","","",""};           
            b1=new JButton("0-9/A-Z");
            b2=new JButton("9-0/Z-A");
            b1.addActionListener(this);
            b2.addActionListener(this);
          combo = new JComboBox(combodata);
          combo.setSelectedIndex(3);
            combo.setEditable(false);
            combo.addActionListener(this);
           Object[][] rowData = {
                {"Dave", "Smith", new Integer(12), new Integer(75)},
                {"Bill", "Johnson", new Integer(14), new Integer(72)},
                {"Sara", "Anderson", new Integer(11), new Integer(92)},
                {"Kara", "Hoyez", new Integer(16), new Integer(83)},
                {"Kyle", "Cruz", new Integer(13), new Integer(84)}};           
    
           f1 = new JFrame();
             table = new JTable(rowData,columnNames);
           f1.setSize(400,170);
           c1 = f1.getContentPane();
           g1 = new GP(); 
           p1 = new JPanel();
           p1.setSize(400,170);
           
    //int[] intArray = new int[] {4, 1, 3, -23};
      //  Arrays.sort(intArray);
    //System.out.println(rowData[2][3]);
        
          
    
          p1.add(table);
       
    
          p1.add(combo);
          p1.add(b1);
          p1.add(b2);
           c1.add(p1);
           
           f1.show();
        }
            
        
    	public static void main(String[] args)
    	{
    	  Main pw = new Main();
         }
         public class GP extends JPanel
    {
       
          
      
          public GP()
        {
      
      
    }  
    }
        public void actionPerformed(ActionEvent event)
        {
    
            if (event.getSource() == b1){
            Object comboselect = combo.getSelectedItem(); 
           // if (comboselect.equals("First Name")){
           //  for(int i = 0;  i< 4; i++){ 
               // String get2d = rowData.get[0][i];
                // get2d = test[i];
                //}
                 ///insertion sort
                     // int[] test = {54,23,7,53,4};
                //for (int index = 0; index < test.length; index++){
                //    int key = test[index];
                 //   int position = index;
                    
                 //   while (position > 0 && test[position -1] > key){
                 //       test[position] = test[position-1];
                 //       position--;
                 //   }
                //    test[position] = key;
              //  }
                //selection sort
                //for (int index = 0; index < numbers.length-1; index++){
                  //  min=index;
                  //  for(int scan = index+1; scan < numbers.length; scan++){
                     //   if (numbers[scan] < numbers [min]){
                      //      min=scan;
                            
                            //swap values
                        //    temp = numbers[min];
                        //    numbeers[min] = numbers[index];
                        //    numbers[index] = temp;
                       // }
                   // }
                //}
                 //for (int index = 1; index < test.length; index++){
                //System.out.println(test[index]);
          //  }
            //}
            if (comboselect.equals("Last Name")){}
            if (comboselect.equals("Age")){}
            if (comboselect.equals("Test Scores")){}
                
            }
           
             if (event.getSource() == b2){
             Object comboselect = combo.getSelectedItem(); 
            if (comboselect.equals("First Name")){}
            if (comboselect.equals("Last Name")){}
            if (comboselect.equals("Age")){}
            if (comboselect.equals("Test Scores")){}  
                
            }  
              
           
             
           
        
        }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    If you are using 1.6 (you should be) then throw everything away and read this.

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      We have to do it the old way.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by carlos123
        We have to do it the old way.
        WHY do we have to do it the old way?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by carlos123
          We have to do it the old way.
          Most of the JTable class already existed in Java 1.3 so using it is also quite the
          'old' way ...

          kind regards,

          Jos

          Comment

          • jayamohan
            New Member
            • Nov 2007
            • 1

            #6

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Hi, can you please tells us what you're having trouble with without dumping all
              that wallpaper of code in our face? In the Java Articles section there's an article
              about a generic heapsort implementation. As far as I can tell from all your code,
              the code in that article can easily do the job.

              kind regards,

              Jos

              Comment

              Working...