I have a JButton
to add data and a second to view data, both are working perfectly but a third JButton
to remove all data from ArrayList
and set the JTextarea
to empty. I tried setting the JTextArea
to "" and listof.remove
but nothing happens when I clicked the JButton
. this is the code.
public class thehandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent ev){
Student student=new Student();
if(ev.getSource()==addData){
if(listof.size() <= 4){
try{
student.setEng(Double.parseDouble(eng.getText().toString()));
student.setScience(Double.parseDouble(sci.getText().toString()));
student.setSocial(Double.parseDouble(soc.getText().toString()));
student.setMath(Double.parseDouble(math.getText().toString()));}
catch(Exception e){
JOptionPane.showMessageDialog(null, "one or more of the subject fields contains invalid inputs,PLEASE NUMBER REQUIRED, please check it", "error", JOptionPane.ERROR_MESSAGE);
return;
}
student.setId(sidInput.getText().toString());
student.setSn(snameInput.getText().toString());
student.setLn(lnameInput.getText().toString());
listof.add(student);
sidInput.setText("");
snameInput.setText("");
lnameInput.setText("");
eng.setText("");
math.setText("");
soc.setText("");
sci.setText("");
}else
{
// if the size exceeds required alert the user
JOptionPane.showMessageDialog(null, "The maximum Number of Student that can be added is 50 ",
"ERROR",JOptionPane.ERROR_MESSAGE);
// and set the id field to no input state abd clear all fields
sidInput.setEditable(false);
sidInput.setText("");
snameInput.setText("");
lnameInput.setText("");
eng.setText("");
math.setText("");
soc.setText("");
sci.setText("");
return;
}
Collections.sort(listof, new sortAverage());
}
else if(ev.getSource()==viewData){
display.setText(student.header());
display.setFont(new Font("serif",Font.BOLD,14));
int x=0;
Student lastStudent =null;
for(Student of:listof){
if(lastStudent == null ||Double.compare(of.getAverage(), lastStudent.getAverage()) !=0){
x++;
}
display.append(of.toString()+"\r\t\r "+of.getPosition(x)+"\r\n");
lastStudent=of;
}
}
else if(ev.getSource()==clear){
listof.remove(student);
display.setText("");
}
}
}
And the data output is inconsistent. I have a fields for studentid, name and the rest but if the name is long it pushes the other data far from their headings and when I change the tabs and space, short names also brings the data from their headings. This is the code for the output and the headings
public String header(){
// returns the header of the ressult
return "STUDENTID \r \t STUDENT FULLNAME \r\tENGLISH \t MATH \t SOCIAL \t SCIENCE "
+ "\tTOTAL \t AVERAGE \t POSITIONS\n";
}
@Override
public String toString(){
// display all the values of the student
return "\r "+getSid()+"\r\t"+getSname()+"\r "+getLname()+
"\t\r \t "+getEng()+"\t\r "+getMath()+"\t\r "+getSocial()+
"\t\r "+getScience()+"\t\r "+getTotalMark()+
"\t\r "+getAverage();
}
Is there any proper way of doing this. Any help would be greatly appreciated.