1

I am new to Swing. I have implemented SwingWorker class but somehow its not displaying the list. Please see below code let me know why its not printing line inside "System.out.println"

Below is the SwingWorker code. I also have the logic for the execute method. When I debug my code its going into "RoutingList rlist = new RoutingList();" line. But After that line it take me to "FutureTask.java" and its method "void innerRun()" where it throws some exception "NoClassDefFound" and it ignores rest of my code.

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();
    }
}

Here is the RoutingList class

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);

    }
}

}

user1483570
  • 51
  • 3
  • 10

1 Answers1

2

If the println() in doInBackground() is not printing, verify that you instantiated the worker and invoked it's execute() method, as outlined below. You probably shouldn't have GUI components in your worker. You can reference your label in an enclosing scope or leverage the same property change mechanism used by setProgress(). Examples maybe found in the API and here.

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

    @Override
    public void propertyChange(PropertyChangeEvent e) {
        // update progress 
    }
});
task.execute();
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I have that logic. Even when I debug my code its going into "RoutingList rlist = new RoutingList();" line. But After that line it take me to "FutureTask.java" and its method "void innerRun()" where it throws some exception "NoClassDefFound" and it ignores rest of my code. – user1483570 Jul 04 '12 at 17:01
  • That sounds like a race for the `routingList`; it may be tough to debug. For reference, you might step through the [sscce](http://sscce.org/) I cited above to see how it's supposed to look. – trashgod Jul 04 '12 at 17:20