SwingWorker : illegal start of type

孤者浪人 提交于 2019-12-11 23:57:21

问题


it is a piece of code that caused my problem :

   SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>(){

     @Override
        protected Vector doInBackground() throws Exception {

             TvaJpaController tjc =new TvaJpaController(emf);
           Vector  l_tva=null;

          try{
         l_tva= (Vector) tjc.findTvaEntities();

             }
        catch(javax.persistence.PersistenceException e)
             {

             javax.swing.JOptionPane.showMessageDialog(null,"please check your internet connecting"); 

             } 
       return l_tva;
     }

     @Override
        protected void done() {
     Vector   l_tva=null;
            try {
                  l_tva=get();
            } catch (InterruptedException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }

         int n =  l_tva.size();
          for(int i=0;i<n;i++){
       Tva tva =(Tva)l_tva.elementAt(i);
   tva_article.addItem(tva.getIdtva());

   }

     }
   };

    sw.execute(); 

this line :

SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>()

gives an error :illegal start of type... I think my problem was due to "vector",but I do not know how to solve.. Any Helps ?


回答1:


No, the problem is the use of void, which isn't a valid type argument. You can use SwingWorker<Vector, Void> though. (Note the difference between void, which is a Java keyword, and Void which refers to the java.lang.Void type.)

Personally I'd suggest using List<E> in preference to explicitly using Vector, and using it generically if possible, with ArrayList<E> as an implementation rather than Vector, but that's a separate matter - it's only the void / Void which is causing you immediate problems.




回答2:


and second issue is that about wrong usage of method get() into SwingWorker's void done(), because returns only exception from SwingWorker (and only if exception exists), more about usage of method get() in my question



来源:https://stackoverflow.com/questions/9229525/swingworker-illegal-start-of-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!