SwingWorker代码问题

时间:2012-07-03 22:08:47

标签: java swing swingworker

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

中的打印行

以下是SwingWorker代码。我也有execute方法的逻辑。当我调试我的代码时,它进入“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);

    }
}

}

1 个答案:

答案 0 :(得分:2)

如果println()中的doInBackground()未打印,请验证您是否已实例化该工作程序并调用它的execute()方法,如下所述。您可能不应该在工作者中使用GUI组件。您可以在封闭范围内引用标签,也可以利用setProgress()使用的相同属性更改机制。可以在APIhere中找到示例。

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

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