我一路搜索但无法找到解决方案。
我想更改进度条中的默认java图标,
正如我使用JProgressBar
。
这是我的小片段
public class SwingWorkerProgressMonitor implements PropertyChangeListener {
private static final int PROGRESS_BAR_WIDTH = 200;
private ProgressMonitor progressMonitor;
private final SwingWorker<?, ?> task;
private SwingWorker<Void, Void> backingTask;
private String message;
private final JProgressBar progressBar = new JProgressBar();
int progress = 0;
private final String title;
/*
* Progress monitor that takes a swing worker and shows its progress
*/
public SwingWorkerProgressMonitor(SwingWorker<?, ?> sw, String title) {
this.task = sw;
this.title = title;
}
/**
* This Fu2nction Invoked when task's progress property changes.
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progress")) {
int progress = (Integer) evt.getNewValue();
updateProgress(progress);
}
}
private void updateProgress(int progress) {
progressMonitor.setProgress(progress);
message = String.format(PlasaApplication.getTranslator().getString(PlasaLanguageKey.PROGRESS_LODING_DATA)
+ "(%d%%)", progress);
progressMonitor.setNote(message);
if (progressMonitor.isCanceled() || task.isDone()) {
Toolkit.getDefaultToolkit().beep();
if (progressMonitor.isCanceled()) {
task.cancel(true);
}
}
}
/**
* This function start the progress When click the save button on
*
*
* @param component
*/
public void start(Component component) {
// creates progress bar
Dimension preferredSize = progressBar.getPreferredSize();
preferredSize.width = PROGRESS_BAR_WIDTH;
progressBar.setPreferredSize(preferredSize);
UIManager.put("ProgressMonitor.progressText", title);
UIManager.put("OptionPane.cancelButtonText",
PlasaApplication.getTranslator().getString(PlasaLanguageKey.GLOBAL_CANCEL));
progressMonitor = new ProgressMonitor(progressBar, "", "", 0, 100);
progressMonitor.setProgress(0);
if (task == null) {
throw new IllegalArgumentException("swing worker is not set");
}
backingTask = new BackingTask(task);
task.addPropertyChangeListener(this);
backingTask.execute();
}
/**
* Inner class Here begin the progress status
*
*/
private class BackingTask extends SwingWorker<Void, Void> {
SwingWorker<?, ?> worker; // backing worker
public BackingTask(SwingWorker<?, ?> worker) {
this.worker = worker;
}
// run the task on background
@Override
protected Void doInBackground() throws Exception {
worker.execute();
return null;
};
/**
* This function close the progress monitor when task is done or cancel
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
progressMonitor.close();
}
}
}
答案 0 :(得分:1)
因此,假设您要更改应用程序的图标(JFrame
)并且您想在进度子类中执行此操作,请尝试以下方法:
public class YourFrame extends JFrame {
public YourFrame() {
... new SwingWorkerProgressMonitor(this, ...);
}
}
public class SwingWorkerProgressMonitor extends ... {
private final JFrame frame;
public SwingWorkerProgressMonitor(JFrame frame, ...) {
this.frame = frame;
...
}
private void setIconInProgress() {
this.frame.setIconImage(...);
}
}