0

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.

ErmIg
  • 3,980
  • 1
  • 27
  • 40
Whiteag
  • 23
  • 5

2 Answers2

0

You are removing your new Student() but he has never been added when clicking the "clear"-button. You have to do listof.clear()

else if(ev.getSource()==clear){
         listof.clear();//removes all entries from the list
         display.setText("");
        }
  }

For aligning the headers i would suggest to use a JTable But if you dont want to use it you can try the following:

create static method in your Student class for aligning the headers. Use static in this case because then its not bound a single Student:

public static String getAlignedHeader(int maxLength){
    // returns the header of the ressult
    String tmpStrg = " STUDENT FULLNAME  ";
    int currentLength = tmpStrg.length();
    for(int i = 0; i < (maxLength-currentLength); i++){
        tmpStrg+=" ";
    }
    return "STUDENTID \r  \t"+tmpStrg+"\r\tENGLISH \t MATH \t SOCIAL \t SCIENCE "+ "\tTOTAL \t AVERAGE  \t POSITIONS\n";
}

Then use it:

display.setFont(new Font("serif",Font.BOLD,14));

int maxLength = Integer.MAX_VALUE;
for (Student student : listof) {
    maxLength = Math.min(maxLength, (student.getSname().length()+getLname().length()));
}
display.setText(Student.getAlignedHeader(maxLength));
//append student as you already do
Jérôme
  • 1,254
  • 2
  • 20
  • 25
  • it worked. thanks, now my problem is getting the output to display without any "push overs". – Whiteag Apr 06 '17 at 22:18
  • Why dont u use a `JTable` then you dont have to handle the spacing by yourself?! – Jérôme Apr 06 '17 at 22:40
  • Updated my answer. But rethink of using `JTable`! – Jérôme Apr 06 '17 at 22:59
  • hi @Jérôme, i tried the jtable is working but any time i output a data and add another data, if i clicked the view button the ones already in the table comes again, thus creating duplicate – Whiteag Apr 07 '17 at 21:45
  • Please look here: http://stackoverflow.com/questions/3179136/jtable-how-to-refresh-table-model-after-insert-delete-or-update-the-data how to handle `JTable`. Try it and if you have problems with it open a New question for it – Jérôme Apr 07 '17 at 23:29
0
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++; 

                   } 

            Object [] row ={of.getSid(),of.getFullname(),of.getEng(),of.getMath(),of.getSocial(),of.getScience(),of.getTotalMark(),of.getAverage(),of.getPosition(x)};

              lastStudent=of; 
              tableModel.addRow(row);
          }
             }
Whiteag
  • 23
  • 5
  • Dont add the code as answer but update your question with it. But do not override the original code. Post this code in an **Update JTable** section – Jérôme Apr 07 '17 at 23:28