Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Class List GUI
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
eNc




PostPosted: Sun Nov 20, 2005 4:03 pm   Post subject: Class List GUI

Ok so in the following code I am trying to change all the JButtons labels. This is how it works. The JButton labels are corresponding to a class list. The class list can be sorted, randomized and searched. I would like to have the class list randomized and displayed when the "Ownage" button is pressed. I have tried to do so in the following code, but I only can get the default class list to appear in the GUI. Please help me to see what I am doing wrong.

code:

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//start class student
class student
{
        //declaring instance fields
        String firstName;       
        String lastName;
        int age;
       
        //student constructor
        public student(String l, String f, int a)
        {
                lastName = l;
                firstName = f;
                age = a;
        }//end student constructor
       
        //Part A - creates createList() method
        //creates class list and store into array studentArray
        //source from studentList.txt
        public static student[] createList()
        {
                String first, last = null;
                int age = 0;
               
                student [] studentArray = new student[33];
               
      try
      {
        FileReader fr = new FileReader("C:\\java\\programs\\chanto\\studentList.txt");
        BufferedReader br = new BufferedReader(fr);   
        String line;
        for(int i = 0; i<=studentArray.length; i++)
        {
         line =br.readLine();
                StringTokenizer st = new StringTokenizer(line,",", false);
                first = st.nextToken();
                last = st.nextToken();
                age = Integer.parseInt(st.nextToken());
                                studentArray[i] = new student(first, last, age);
         }
         br.close();
                }
      catch (Exception y){}
                return studentArray;
        }//end createList() method
       
        // Part B - creates printstudentArray method
        // display students' names and ages to screen (First Name, Last Name then "age" then Age)
        public static void printstudentArray(student[] sArray)
        {
                        for (int i = 0; i < sArray.length; i++)
                                Output.println ( sArray[i].firstName + " " + sArray[i].lastName + ", age " + sArray[i].age);
        }//end printstudentArray method        
       
        // Part C - creates random method
        //rearranges the students in the list into random order
        public static student[] random (student[] studentArray)
        {
                student temp = new student("","",0);
                for(int i = 0; i<studentArray.length; i++)
                {
                        int randNum = (int)(Math.random()*studentArray.length);
                        temp = studentArray[i];
                        studentArray[i] = studentArray[randNum];
                        studentArray[randNum] = temp;
                }              
                return studentArray;
        }//end random method
       
        //creates compareTo method
        //distinguish one student being "bigger", "smaller" or "equal" to another
        //essential for searching and sorting
        public int compareTo(Object o)
        {
                student s = (student) o;
                int result = 0;
                result = this.lastName.compareTo(s.lastName);
                if(result != 0)
                        return result;
                result = this.firstName.compareTo(s.firstName);
                if(result != 0)
                        return result;
                result = this.age-s.age;
                return result;
        }//end compareTo method

        //Part D - creates seqSearch method
        //Find a student using sequential search
        //(Can do whether or not the class list is sorted)
        public static int seqSearch(student[] studentArray, student item)
        {
                int location= -1;
                for (int i=0;i<studentArray.length;i++)
                        if (studentArray[i].compareTo(item)==0)
                                location =i;
                        return location;
        }//end seqSearch method
       
        //Part F - creates binSearch method
        //Find a student using binary search
        //(class list must be sorted for it to work properly)
        public static int binSearch (student[] studentArray, student item)
        {
                int bottom=0;
                int top= studentArray.length-1;
                int middle;
                boolean found = false;
                int location = -1;
       
                while (bottom<= top & !found)
                {
                        middle = (bottom+top)/2;
                        if (studentArray[middle].compareTo(item)==0)
                        {
                                found = true;
                                location = middle;
                        }
                        else if (studentArray[middle].compareTo(item)<0)
                                bottom=middle+1;
                        else
                                top = middle-1;
                }
                return location;
        }// end binSearch method
       
        //creates findK method
        //calculate the best value for k
        //(essential to shell sort)
        public static int findK(int n)
        {
                int k = 1;
                while (k * 3 + 1 < n)
                        k = k * 3 + 1;
                return k;
        }//end findK method
       
        //Part G - creates shellSort method
        //Sort the class list
        public static student[] shellSort (student[] studentArray)
        {
                for (int k = findK(studentArray.length); k > 0; k = (k-1) / 3)
                        for (int loop = 0; loop < k; loop++)
                                for (int top = k + loop; top < studentArray.length; top += k)
                                {
                                        int i;
                                        student item = studentArray[top];
                                        for (i = top; i > 0 + loop && item.compareTo(studentArray[i-k]) < 0; i -= k)
                                                studentArray[i] = studentArray[i-k];
                                        studentArray[i] = item;
                                }
                return studentArray;
        }
}




class Assignment02
{
        public static void main(String[] args)
        {
                classFrame f = new classFrame();
        }
}

class classFrame extends JFrame implements ActionListener
{
        student[] sArray = student.createList();
        DrawArray buttonsArray;
       
        public classFrame()
        {
                super("Title");
                                               
                JPanel cpane = (JPanel) getContentPane();
                cpane.setLayout(new BorderLayout());
               
                buttonsArray = new DrawArray(sArray);
                cpane.add(buttonsArray.buttonsPane, BorderLayout.CENTER);
               
                JButton button33 = new JButton("Ownage");
                button33.addActionListener(this);
                cpane.add(button33, BorderLayout.SOUTH);
               
                setSize(1000,300);
                setVisible(true);
        }
       
        public void actionPerformed(ActionEvent e)
        {
                if(e.getActionCommand().equals("Owange"))
                {
                        sArray = student.random(student.createList());
                        buttonsArray = new DrawArray(sArray);
                        repaint();
                }
        }
}

class DrawArray extends JComponent
{
        JPanel buttonsPane;

        public student[] setArray(student[] array)
        {
                student[] studentArray = array;
                return studentArray;
        }       
       
        public DrawArray(student[] studentArray)
        {
           buttonsPane = new JPanel();
                buttonsPane.setLayout(new GridLayout(5,6));
               
                JButton button00 = new JButton(studentArray[0].firstName+" "+studentArray[0].lastName );
                buttonsPane.add(button00);     
                JButton button01 = new JButton(studentArray[1].firstName+" "+studentArray[1].lastName );
                buttonsPane.add(button01);
                JButton button02 = new JButton(studentArray[2].firstName+" "+studentArray[2].lastName );
                buttonsPane.add(button02);
                JButton button03 = new JButton(studentArray[3].firstName+" "+studentArray[3].lastName );
                buttonsPane.add(button03);
                JButton button04 = new JButton(studentArray[4].firstName+" "+studentArray[4].lastName );
                buttonsPane.add(button04);
                JButton button05 = new JButton(studentArray[5].firstName+" "+studentArray[5].lastName );
                buttonsPane.add(button05);
                JButton button06 = new JButton(studentArray[6].firstName+" "+studentArray[6].lastName );
                buttonsPane.add(button06);
                JButton button07 = new JButton(studentArray[7].firstName+" "+studentArray[7].lastName );
                buttonsPane.add(button07);
                JButton button08 = new JButton(studentArray[8].firstName+" "+studentArray[8].lastName );
                buttonsPane.add(button08);
                JButton button09 = new JButton(studentArray[9].firstName+" "+studentArray[9].lastName );
                buttonsPane.add(button09);
                JButton button10 = new JButton(studentArray[10].firstName+" "+studentArray[10].lastName );
                buttonsPane.add(button10);           
                JButton button11 = new JButton(studentArray[11].firstName+" "+studentArray[11].lastName );
                buttonsPane.add(button11);
                JButton button12 = new JButton(studentArray[12].firstName+" "+studentArray[12].lastName );
                buttonsPane.add(button12);
                JButton button13 = new JButton(studentArray[13].firstName+" "+studentArray[13].lastName );
                buttonsPane.add(button13);
                JButton button14 = new JButton(studentArray[14].firstName+" "+studentArray[14].lastName );
                buttonsPane.add(button14);
                JButton button15 = new JButton(studentArray[15].firstName+" "+studentArray[15].lastName );
                buttonsPane.add(button15);
                JButton button16 = new JButton(studentArray[16].firstName+" "+studentArray[16].lastName );
                buttonsPane.add(button16);
                JButton button17 = new JButton(studentArray[17].firstName+" "+studentArray[17].lastName );
                buttonsPane.add(button17);
                JButton button18 = new JButton(studentArray[18].firstName+" "+studentArray[18].lastName );
                buttonsPane.add(button18);
                JButton button19 = new JButton(studentArray[19].firstName+" "+studentArray[19].lastName );
                buttonsPane.add(button19);
                JButton button20 = new JButton(studentArray[20].firstName+" "+studentArray[20].lastName );
                buttonsPane.add(button20);           
                JButton button21 = new JButton(studentArray[21].firstName+" "+studentArray[21].lastName );
                buttonsPane.add(button21);
                JButton button22 = new JButton(studentArray[22].firstName+" "+studentArray[22].lastName );
                buttonsPane.add(button22);
                JButton button23 = new JButton(studentArray[23].firstName+" "+studentArray[23].lastName );
                buttonsPane.add(button23);
                JButton button24 = new JButton(studentArray[24].firstName+" "+studentArray[24].lastName );
                buttonsPane.add(button24);
                JButton button25 = new JButton(studentArray[25].firstName+" "+studentArray[25].lastName );
                buttonsPane.add(button25);
                JButton button26 = new JButton(studentArray[26].firstName+" "+studentArray[26].lastName );
                buttonsPane.add(button26);
                JButton button27 = new JButton(studentArray[27].firstName+" "+studentArray[27].lastName );
                buttonsPane.add(button27);
                JButton button28 = new JButton(studentArray[28].firstName+" "+studentArray[28].lastName );
                buttonsPane.add(button28);
                JButton button29 = new JButton(studentArray[29].firstName+" "+studentArray[29].lastName );
                buttonsPane.add(button29);
                JButton button30 = new JButton(studentArray[30].firstName+" "+studentArray[30].lastName );
                buttonsPane.add(button30);
                JButton button31 = new JButton(studentArray[31].firstName+" "+studentArray[31].lastName );
                buttonsPane.add(button31);
                JButton button32 = new JButton(studentArray[32].firstName+" "+studentArray[32].lastName );
                buttonsPane.add(button32);
                repaint();
        }
}


Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: