删除我的arraylist中的所有项目并将JTextarea设置为空

时间:2017-04-06 21:56:19

标签: java arraylist jtextarea

我有一个JButton来添加数据,第二个用于查看数据,两者都运行良好但只有三分之一JButtonArrayList删除所有数据并设置JTextarea } 清空。我尝试将JTextArea设置为“”和listof.remove但是当我点击JButton时没有任何反应。这是代码。

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("");
            }
      }
     }

数据输出不一致。我有一个artsntid,name和其他字段,但如果名称很长,它会将其他数据推到远离标题的位置,当我更改标签和空格时,短名称也会从其标题中提取数据。这是输出和标题的代码

 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();
          }

有没有正确的方法来做到这一点。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您正在删除new Student(),但在点击“清除”按钮时从未添加过。你必须listof.clear()

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

为了对齐标题,我建议使用JTable 但如果您不想使用它,您可以尝试以下方法:

static类中创建Student方法以对齐标题。在这种情况下使用static,因为它没有绑定一个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";
}

然后使用它:

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

答案 1 :(得分:0)

SELECT * 
FROM MY_TABLE 
WHERE MY_FIELD NOT IN ('AAA','BBB','CCC', 'DDD');