1

我是 Swing 的新手。我已经实现了 SwingWorker 类,但不知何故它没有显示列表。请看下面的代码让我知道为什么它不在“System.out.println”中打印行

下面是 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);

    }
}

}

4

1 回答 1

2

如果println()indoInBackground()没有打印,请验证您是否实例化了 worker 并调用了它的execute()方法,如下所述。您的工作人员中可能不应该有 GUI 组件。您可以在封闭范围内引用您的标签,或利用setProgress(). 示例可在API此处找到

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

    @Override
    public void propertyChange(PropertyChangeEvent e) {
        // update progress 
    }
});
task.execute();
于 2012-07-04T02:17:22.193 回答