我有一个ProgressMonitor,在处理某些数据库表时显示。首先不知道表的数量,所以我创建了最大值为100的ProgressMonitor

一旦在SwingWorker线程中开始处理,它将获取表的数量并将ProgressMonitor的最大值设置为表的数量(在我的测试中约为530)。但是,即使最大值已增加并且进度条准确显示100/530,ProgressMonitor仍以100结尾并停止线程执行。

知道为什么会这样吗?

从按钮按下actionListener以启动ProgressMonitorSwingWorker(名为TaskThread):

public void actionPerformed(ActionEvent e)
{
    String cmd = e.getActionCommand();

    if (cmd.equals("generate")) {
        progressMonitor = new ProgressMonitor(this, "Generating...", "", 0, 100);
        task = new TaskThread();
        task.addPropertyChangeListener(this);
        task.execute();
        genButton.setEnabled(false);
    }
}


PropertyChangeListener更新ProgressMonitor

public void propertyChange(PropertyChangeEvent evt)
{
    if ("progress" == evt.getPropertyName() )
    {
        int progress = (Integer) evt.getNewValue();
        progressMonitor.setProgress(progress);
        String message = String.format("Completed %d%%.\n", progress);
        progressMonitor.setNote(message);
        if (progressMonitor.isCanceled() || task.isDone())
        {
            Toolkit.getDefaultToolkit().beep();
            if (progressMonitor.isCanceled()) {
                task.cancel(true);
            }
            genButton.setEnabled(true);
        }
    }
}


SwingWorker类:

class TaskThread extends SwingWorker<Void, Void>
{
    @Override
    public Void doInBackground()
    {
        String tableList = tablesArea.getText().trim();
        script = "";
        String filename = outPath.getText

        // Connect to Database
        Connection conn = getDbConnection();
        if (conn == null)
            return null;

        try {
            // if tableList is blank, get all tables from the From Database afm_tbls table
            if (tableList.equals(""))
            {
                // code omitted
            }
        }
        catch (SQLException se) {
            canWriteFile = false;
            handleError(se);
        }

        String[] tables = tableList.split(",");
        generateRecordCount(conn, tables);

        // Close the connection
        if(conn != null)
        {
            try {
                conn.close();
            }
            catch (Exception ignored) {}
        }

        // Write SQL code to a file
        writeScript(filename);

        return null;
    }

    private void generateRecordCount(Connection conn, String[] tables)
    {
        setProgressMax(tables.length);
        setProgress(0);

        // Loop through tables
        for (int i = 0; i < tables.length; i++)
        {
            // Get record count for each table
            try {
                String sql = "SELECT COUNT(*) AS reccount FROM " + tables[i];
                Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                ResultSet rs = stmt.executeQuery(sql);
                rs.next();

                int recCount = rs.getInt("reccount");
                if (recCount > 0)
                    script += tables[i] + ": " + recCount + nl;

                // Set progress of SwingWorker, ProgressMonitor is updated by PropertyChangeListener
                int prog = getProgress() + 1;
                setProgress(prog);
            }
            catch(SQLException se)
            {
                handleError(se);
            }
        }
    }
}

最佳答案

我弄清楚了原因。因此,希望对别人有利,这是问题所在:

SwingWorker.setProgress()函数的边界为0和100,因此,即使ProgressMonitor可以处理更高的最大值,当值达到101时,SwingWorker线程也会以IllegalArgumentException结束。

09-08 06:24