2017-02-03 70 views
-1

我正在編寫一個程序,其中我將一個名稱與一個列表進行匹配。如果我沒有找到匹配項,我在SQLite數據庫上查找名稱,將名稱信息提取到文本字段中,然後顯示該卡(cardlayout)。當我運行這個時,我只能得到最後一次不匹配的信息。在研究這個時,我想我可以使用SwingWorker來解決這個問題。我把我的做,並把它放在一個SwingWorker類,並添加一個發佈()我想顯示的信息。我得到相同的結果,只顯示最後一個不匹配。Java SwingWorker未按預期工作

這裏是我的代碼片段:

public class processTeeTimes extends SwingWorker <Integer, String> 
    { 

     @Override 
     protected Integer doInBackground() throws Exception 
     { 
     // Process all tee times foursome at a time 

      String memberGuest;          // Member or guest 
      do              // Process players in foursome 
      { 

       . 
       . 
       . 


        if (!firstOnGHIN()) 
        { 
         System.out.print("No match - first (" + tsTime + "): "); 
         printGolfer();      // Print no match 
         publish(golfer[0]); 

        } 

       . 
       . 
       . 

      } 
      while (!EOF) 


     } 

     @Override 
     protected void process(List<String> golferList) 
     { 
      for (int gIndex = 0; gIndex < golferList.size(); gIndex++) 
      { 
       textFieldRMLast.setText(golferList.get(gIndex)); 
       cards.show(panelCont, ROSTERMAINT); // Show roster maint card 
      } 
     } 

     @Override 
     protected void done() 
     { 
      . 
      . 
      . 

      System.out.println("All done processing Tee Sheet"); 
     } 

任何幫助,將不勝感激。

+2

我們不能編譯也不能運行代碼片段,我們不希望整個代碼庫。相反,我強烈建議您創建併發布有效的[mcve] - 請閱讀鏈接。 –

回答

4

我得到相同的結果,只顯示最後一個不匹配。

因爲這顯示你的代碼做:

@Override 
    protected void process(List<String> golferList) 
    { 
     for (int gIndex = 0; gIndex < golferList.size(); gIndex++) 
     { 
      textFieldRMLast.setText(golferList.get(gIndex)); 
      cards.show(panelCont, ROSTERMAINT); // Show roster maint card 
     } 
    } 

你將數據放置到一個JTextField,並且遇到更多的數據分鐘,立即與新的取代了以往的文字文本。如果您需要顯示多行數據,那麼您應該使用顯示多行數據的組件,例如JList或JTable。

如果你想觀看JTextField中的數據之間的延遲,則考慮把一個Thread.sleep(...)在do-while循環被稱爲在doInBackground()方法。因爲上面的代碼是在Swing事件線程上調用的,所以不要在上面的for循環中放置Thread.sleep

並注意,如果這個答案不回答你的問題,那麼是的,你會想創建併發布有效的MCVE

+0

就是這樣。謝謝。 –