2011-07-08 25 views
0

我一直在試圖熟悉SwingWorkers在Java中的使用。是否可以觸發一個對話框在SwingWorker的過程方法中從GUI顯示?如何用SwingWorker(外部類)觸發對話框

+1

你嘗試了嗎? ;) – Bozho

+0

@Titus,到目前爲止你做了什麼?此外,['SwingWorker'](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html)API中有一個示例,它向您展示瞭如何完成這項工作,甚至儘管這很愚蠢。 – mre

+0

@Titus:這[示例](http://stackoverflow.com/questions/4637215/can-a-progress-bar-be-used-in-a-class-outside-main/4637725#4637725)補充了[ API](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html)示例。 – trashgod

回答

2

一個的SwingWorker的過程方法

如果你的意思內:

一)內部doInBackground(),

public Boolean doInBackground() { 

    // caught exception half way e.g. login auth fail 

    // 'push' into GUI layer (EDT) 
    String text = "Your error here"; 
    publish(text); 

    Boolean result = Boolean.TRUE; 

    // continue with remaining process 

    return result; 
} 

protected void process(List<Object> chunks) { 
    String[] message = chunks.toArray(new String[chunks.size()]); 

    // prompt user with dialog box 
    JDialogBox.showMessageDialog(window, message); 
} 

B)中完成的(),

protected void done() { 
    // prompt user with dialog box 
    String message; 
    try { 
    message = get(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    JDialogBox.showMessageDialog(window, message); 
} 

c)or outside get()?

SwingWorker sw = new SwingWorker() {.....; 
sw.execute(); 

// some time later 

final String message; 
try { 
    message = sw.get(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

Runnable edt = new Runnable() { 

    public void run() { 
    JDialogBox.showMessageDialog(window, message); 
    } 
} 

java.awt.EventQueue.invokeLater(edt); 
1

肯定的是,可以通過工具PropertyChangeListener(),有兩種實現(以避免的方法錯了實現)

import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 

import javax.swing.*; 

public class TestProgressBar { 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("TestProgressBar"); 
     frame.getContentPane().add(new TestPBGui().getMainPanel()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 

    private TestProgressBar() { 
    } 
} 

class TestPBGui { 

    private JPanel mainPanel = new JPanel(); 

    public TestPBGui() { 
     JButton yourAttempt = new JButton("Your attempt to show Progress Bar"); 
     JButton myAttempt = new JButton("My attempt to show Progress Bar"); 
     yourAttempt.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       yourAttemptActionPerformed(); 
      } 
     }); 
     myAttempt.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       myAttemptActionPerformed(); 
      } 
     }); 
     mainPanel.add(yourAttempt); 
     mainPanel.add(myAttempt); 
    } 

    private void yourAttemptActionPerformed() { 
     Window thisWin = SwingUtilities.getWindowAncestor(mainPanel); 
     JDialog progressDialog = new JDialog(thisWin, "Uploading..."); 
     JPanel contentPane = new JPanel(); 
     contentPane.setPreferredSize(new Dimension(300, 100)); 
     JProgressBar bar = new JProgressBar(0, 100); 
     bar.setIndeterminate(true); 
     contentPane.add(bar); 
     progressDialog.setContentPane(contentPane); 
     progressDialog.pack(); 
     progressDialog.setLocationRelativeTo(null); 
     Task task = new Task("Your attempt"); 
     task.execute(); 
     progressDialog.setVisible(true); 
     while (!task.isDone()) { 
     } 
     progressDialog.dispose(); 
    } 

    private void myAttemptActionPerformed() { 
     Window thisWin = SwingUtilities.getWindowAncestor(mainPanel); 
     final JDialog progressDialog = new JDialog(thisWin, "Uploading..."); 
     JPanel contentPane = new JPanel(); 
     contentPane.setPreferredSize(new Dimension(300, 100)); 
     final JProgressBar bar = new JProgressBar(0, 100); 
     bar.setIndeterminate(true); 
     contentPane.add(bar); 
     progressDialog.setContentPane(contentPane); 
     progressDialog.pack(); 
     progressDialog.setLocationRelativeTo(null); 
     final Task task = new Task("My attempt"); 
     task.addPropertyChangeListener(new PropertyChangeListener() { 

      @Override 
      public void propertyChange(PropertyChangeEvent evt) { 
       if (evt.getPropertyName().equalsIgnoreCase("progress")) { 
        int progress = task.getProgress(); 
        if (progress == 0) { 
         bar.setIndeterminate(true); 
        } else { 
         bar.setIndeterminate(false); 
         bar.setValue(progress); 
         progressDialog.dispose(); 
        } 
       } 
      } 
     }); 
     task.execute(); 
     progressDialog.setVisible(true); 
    } 

    public JPanel getMainPanel() { 
     return mainPanel; 
    } 
} 

class Task extends SwingWorker<Void, Void> { 

    private static final long SLEEP_TIME = 4000; 
    private String text; 

    public Task(String text) { 
     this.text = text; 
    } 

    @Override 
    public Void doInBackground() { 
     setProgress(0); 
     try { 
      Thread.sleep(SLEEP_TIME);// imitate a long-running task 
     } catch (InterruptedException e) { 
     } 
     setProgress(100); 
     return null; 
    } 

    @Override 
    public void done() { 
     System.out.println(text + " is done"); 
     Toolkit.getDefaultToolkit().beep(); 
    } 
} 
相關問題