2012-07-03 57 views
1

我是Swing的新手。我已經實現了SwingWorker類,但不知何故它不顯示列表。請參閱下面的代碼讓我知道爲什麼它不打印行內「System.out.println」SwingWorker代碼問題

以下是SwingWorker代碼。我也有執行方法的邏輯。當我調試我的代碼進入「RoutingList rlist = new RoutingList();」線。但是在這之後,它將我帶到「FutureTask.java」及其方法「void innerRun()」,它引發了一些異常「NoClassDefFound」,它忽略了我的代碼的其餘部分。

private class RoutingDataLoader extends javax.swing.SwingWorker<List<RoutingBean>, RoutingBean> { 
    private final RoutingTableModel routingTableModel; 
    private final List<RoutingBean> routings = new ArrayList<RoutingBean>(); 
    private JLabel credits; 

    private RoutingDataLoader(RoutingTableModel tableModel) { 
     this.routingTableModel = tableModel; 
    } 

    @Override 
    public List<RoutingBean> doInBackground() { 

     RoutingList rlist = new RoutingList(); 
     List<RoutingBean> list = rlist.getRoutingList(); 

     System.out.println("Routing Size: " + list.size()); 

     for (int i = 0; i < list.size(); i++) { 
      RoutingBean bean = list.get(i); 
      routings.add(bean); 
      if (routings.size() % 3 == 0) { 
       try { // slow it down so we can see progress :-) 
        Thread.sleep(1); 
       } catch (Exception ex) { 
       } 
      } 
      publish(bean); 
      setProgress(100 * (i+1)/list.size()); 

     } 
     return routings; 
    } 

    //@Override 
    protected void process(List<RoutingBean> moreRoutings) { 
     if (credits == null) { 
      showCredits(); 
     } 
     routingTableModel.add(moreRoutings); 
    } 

    // For older Java 6 on OS X 
    protected void process(RoutingBean... moreRoutings) { 
     for (RoutingBean routing : moreRoutings) { 
      routingTableModel.add(routing); 
     } 
    } 

    private void showCredits() { 
     credits = new JLabel(getString("DataSheetTable.credits", 
       "<html><p align=\"center\">Academy Award data<br>courtesy of Howard Katz</p></html>")); 
     credits.setFont(UIManager.getFont("Table.font").deriveFont(24f)); 
     credits.setHorizontalAlignment(JLabel.CENTER); 
     credits.setBorder(new CompoundBorder(new TitledBorder(""), 
       new EmptyBorder(20, 20, 20, 20))); 
     dataPanel.showMessageLayer(credits, .75f); 
    } 

    @Override 
    protected void done() { 
     setProgress(100); 
     dataPanel.hideMessageLayer(); 
    } 
} 

這裏是RoutingList類

public class RoutingList { 

private List<RoutingBean> routingList; 

public RoutingList(List<RoutingBean> routingList) { 
    this.routingList = routingList; 
} 

public RoutingList() { 
    if (this.routingList == null) { 
     this.routingList = new ArrayList<RoutingBean>(0); 
    } 
    retrieveList(); 
} 

public List<RoutingBean> getRoutingList() { 
    return routingList; 
} 

public void setRoutingList(List<RoutingBean> routingList) { 
    this.routingList = routingList; 
} 



public void retrieveList() { 

    DecimalFormat myFormatter = new DecimalFormat("0000"); 

    for (int i = 0; i < 100; i++) { 
     RoutingBean bean = new RoutingBean(); 

     String number = myFormatter.format(i); 

     OrganizationBean homeOrg = new OrganizationBean(); 
     homeOrg.setOrganization_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     homeOrg.setOrganization_name("HomeOrg_" + number); 
     bean.setHome_organization_bean(homeOrg); 

     DivisionBean homeDiv = new DivisionBean(); 
     homeDiv.setDivision_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     homeDiv.setDivision_name("HomeDiv_" + number); 
     bean.setHome_division_bean(homeDiv); 

     OrganizationBean partOrg = new OrganizationBean(); 
     partOrg.setOrganization_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     partOrg.setOrganization_name("PartOrg_" + number); 
     bean.setPartner_organization_bean(partOrg); 

     DivisionBean partDiv = new DivisionBean(); 
     partDiv.setDivision_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     partDiv.setDivision_name("PartDiv_" + number); 
     bean.setPartner_division_bean(partDiv); 

     TransactionSetBean transBean = new TransactionSetBean(); 
     transBean.setTransactionSet_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     transBean.setTransactionSet_name("TransSet_" + number); 
     bean.setTransactionSet_bean(transBean); 

     DocumentStandardBean docStdBean = new DocumentStandardBean(); 
     docStdBean.setDocumentStandard_identifier(GetUniqueIdentifier.getUniqueIdentifier()); 
     docStdBean.setDocumentStandard_name("DocStd_" + number); 
     bean.setDocumentStandard_bean(docStdBean); 


     bean.setMap_name("MAP_" + number); 
     bean.setCommunication_identifier("Communication_" + number); 
     bean.setActive_flag("Active"); 
     bean.setMode("Deferred"); 

     this.routingList.add(bean); 

    } 
} 

}

+0

Thread.sleep(1);不是一秒鐘而是幾毫秒,這個睡眠在每個本地操作系統的硬件和軟件延遲下(使用GUI界面) – mKorbel

+0

@mKorbel - 爲什麼不打印System.out.println()。 – user1483570

+3

我不知道,對不起,我是志願者,沒有打屁股,爲了更好的幫助,儘快發佈[SSCCE](http://sscce.org/) – mKorbel

回答

2

如果doInBackground()println()不打印,請確認您實例化的工人,並調用它的​​方法,概述如下。你的工作人員可能不應該有GUI組件。您可以在封閉範圍內引用您的標籤,或利用setProgress()所使用的相同屬性更改機制。例子可以在APIhere中找到。

RoutingDataLoader task = new RoutingDataLoader(tableModel); 
task.addPropertyChangeListener(new PropertyChangeListener() { 

    @Override 
    public void propertyChange(PropertyChangeEvent e) { 
     // update progress 
    } 
}); 
task.execute(); 
+0

我有這個邏輯。即使當我調試我的代碼時,它會進入「RoutingList rlist = new RoutingList();」線。但是在這之後,它將我帶到「FutureTask.java」及其方法「void innerRun()」,它引發了一些異常「NoClassDefFound」,它忽略了我的代碼的其餘部分。 – user1483570

+0

這聽起來像是'routingList'的競賽;調試可能很難。作爲參考,你可以通過我上面引用的[sscce](http://sscce.org/)來看看它應該看起來如何。 – trashgod