SwingWorker在ActionListener中不起作用

时间:2015-06-10 22:46:35

标签: java user-interface actionlistener swingworker

在我的电子邮件程序的一部分中,用户可以通过下拉列表JComboBox选择要打开的邮件文件夹。此过程最多可能需要20秒,具体取决于文件夹中的消息数量,在此之前只会冻结程序。这是不可取的,所以我使用SwingWorker在GUI的背景中运行文件夹的打开,并在访问文件夹时显示进度条。

使用ActionListener实现文件夹的访问,该String侦听所选的文件夹名称,并将选定的ActionListener设置为要访问的文件夹的名称。以下是 box = new JComboBox<String>(items); box.setEditable(false); box.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String selected = (String)box.getSelectedItem(); if(selected.equals("Choose Folder")) accessFolder(null); else if(selected.equals("Inbox")) accessFolder("INBOX"); else { selected = TAG + selected; accessFolder(selected); } } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Error: " + ex.getClass().getName(), "Error", JOptionPane.ERROR_MESSAGE); } } }); 的代码,其中items是文件夹名称数组。

accessFolder

此代码的最终结果符合预期:所有消息的列表将添加到主框架的一部分。但进度条永远不会出现。但是,如果我将ActionListener方法的代码直接添加到main方法(将accessFolder添加到组合框中),则进度条会显示整个进程,我得到了同样的最终结果。这是private void accessFolder(String folderName) throws MessagingException, IOException { MessagePanel mPanel = new MessagePanel(username, password, folderName); panel4 = mPanel.getJPanel(); panel4.setBorder(BorderFactory.createLoweredBevelBorder()); BorderLayout layout = (BorderLayout)panel3.getLayout(); panel3.remove(layout.getLayoutComponent(BorderLayout.EAST)); panel3.add(panel4, BorderLayout.EAST); frame.pack(); frame.revalidate(); } 代码。

MessagePanel

所以我似乎无法弄清楚为什么SwingWorker(在accessFolder中的ActionListener类中实现)在代码中的任何地方都可以正常工作,除了SwingWorker。我似乎无法在任何地方找到public class FolderWorker extends CMailWorker { private String username; private String password; private String foldername; private FolderContents accessor; private ArrayList<MailList> msgList; private DefaultListModel<String> model; private ProgressBar bar; private final static String PENDING = "Accessing..."; public FolderWorker(String user, String pass, String fName) { super(PENDING, null); this.username = user; this.password = pass; this.foldername = fName; } protected Void doInBackground() { try { SwingUtilities.invokeLater(new Runnable() { public void run() { try { createProgressBar(); } catch(Exception ex) { setException(ex); } } }); super.throwException(); accessor = new FolderContents(username, password, foldername); model = new DefaultListModel<String>(); msgList = accessor.getMessageList(); for(int x = msgList.size()-1; x>=0; x--) { MailList mList = msgList.get(x); String sub = mList.getSubject(); model.addElement(sub); } return null; } catch(final Exception ex) { SwingUtilities.invokeLater(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, "Error: " + ex.getClass().getName(), "Error", JOptionPane.ERROR_MESSAGE); } }); super.setError(true); return null; } } public ArrayList<MailList> getList() { return accessor.getMessageList(); } public DefaultListModel<String> getModel() { return model; } } 和听众之间的冲突,我不明白为什么会这样,让我完全不知所措。感谢您的任何意见。

编辑:这是SwingWorker代码。

public class CMailWorker extends SwingWorker<Void, Void>
{
    private ProgressBar bar;
    private String completed;
    private String message;
    private boolean error;
    private JFrame frame;
    private Exception exception;
    public CMailWorker(String msg, String done)
    {
        completed = done;
        message = msg;
        error = false;
    }
    protected Void doInBackground()
    {
        return null;
    }
    protected void done()
    {
        bar.dispose();
        if(frame != null)
            killJFrame();
        if(!error && completed != null)
            JOptionPane.showMessageDialog(null, completed);
    }
    protected void setJFrame(JFrame f)
    {
        frame = f;
    }
    private void killJFrame()
    {
        frame.dispose();
    }
    protected void createProgressBar() throws InterruptedException
    {
        bar = new ProgressBar(message);
    }
    protected void setError(boolean e)
    {
        error = e;
    }
    protected boolean getError()
    {
        return error;
    }
    protected void throwException() throws Exception
    {
        if(exception != null)
            throw exception;
    }
    protected void setException(Exception e)
    {
        exception = e;
    }
    protected Exception getException()
    {
        return exception;
    }
}

该课程扩展了这门课程。

Intent intent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
stackBuilder.startActivities();

0 个答案:

没有答案