hi all i need help with my code i'm designing a frame that will ask the user to enter name, age , and id then using add button all details will be entered and displayed in text area at the same frame, using sort button, the details should be order according to the age and display the orderd details again in the text area
i do not know how to sort, and i wrote the code but it does not work
i but the code in 2 packeges this one in GUI package package GUI;
pnlStudentDetails = new JPanel(); pnlStudentDetails.setLayout(new GridLayout (3,2,2,8)); lblName = new JLabel ("Student Name:"); lblID = new JLabel ("Student ID:"); txtName = new JTextField(5); txtID = new JTextField(5); lblAge = new JLabel("Student Age:"); txtAge = new JTextField(5);
//panel with LineBorder pnlStudentDetails.setBorder(new LineBorder(Color.BLACK,2));
// setting the text color to red lblName.setForeground(Color.RED); lblID.setForeground(Color.RED); lblAge.se tForeground(Color.RED); pnlStudentDetails.add(lblName); pnlStudentDetails.add(tx tName); pnlStudentDetails.add(lblID); pnlStudentDetails.add(txtID); pnlStudentDe tails.add(lblAge); pnlStudentDetails.add(txtAge);
pnlTxtArea =new JPanel(); pnlTxtArea.setLayout(new GridLayout(1,1,2,2)); pnlTxtArea.setBorder(new LineBorder(Color.BLACK,2)); pnlTxtArea.add(jtaStudentsList = new JTextArea("",1,2)); JScrollPane scrpn = new JScrollPane (jtaStudentsList); jtaStudentsList.setLineWrap(true); //jtaStudentsList.setFont(new Font ("Courier", Font.BOLD,14)); jtaStudentsList.setEditable(false); pnlTxtArea.add(scrpn);
// panel for the buttons pnlButtons = new JPanel(); pnlButtons.setLayout(new FlowLayout()); pnlButtons.add(btnAdd = new JButton("Add")); pnlButtons.add(btnSort = new JButton("Sort")); pnlButtons.add(btnExit = new JButton("Exit"));
// adding tool tip text to all the buttons btnAdd.setToolTipText("click to add student details to the list"); btnSort.setToolTipText("sort the students in the list according to their age"); btnExit.setToolTipText("Exit from the frame");
// action listener for the exit button btnExit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0);
} });
// action listener for the reset button btnSort.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ btnSortClicked(); } }
);
// action listener for the add button btnAdd.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ btnAddClicked(); } }
public class StudentsDetails { private String name; private String id; private int age;
public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getId() { return id; } // setter for ID public void setId(String id) { this.id = id; } // getter for the studen name public String getName() { return name; } //setter for the student name public void setName(String name) { this.name = name; }
public static ArrayList students = new ArrayList (); public static Set <StudentsDetails> treeSet = new TreeSet<StudentsDetails>(new SortByAge());
public StudentsDetails(String name,String id, int age){ this.name=name; this.id=id; this.age= age; }
public String toString(){ return "Student ID : "+getId()+" Student Name : "+getName()+ " Age : "+getAge();
}
}
the second called SortByAge
import java.util.Comparator;
public class SortByAge implements Comparator <StudentsDetails>, java.io.Serializable{
public int Compare(StudentsDetails s1 , StudentsDetails s2){ int age1=s1.getAge(); int age2=s2.getAge();
if (age1 > age2)
return 1;
else if (age1==age2) return 0;
else return -1; }
public int compare(StudentsDetails a, StudentsDetails b) {