-3

I searched all the way but unable to find the solution. I want to change the default java icon in the progress bar, as I used JProgressBar.

Here is my small snippet

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

}

}

user2914906
  • 21
  • 1
  • 4
  • 3
    Where is the JProgressBar? What icon are you talking about? Can you provide a small example that demonstrates what you're doing? – Kevin Workman Jan 23 '14 at 13:51
  • Stackoverflow is not a tutorial site, it's more of an error oriented helper. You must provide us with your approach on the problem and we will gladly help you figure it out. We will not say 'here, do this', we will say 'instead of that line, try this' or ' have you considered trying zzz instead of yyy'? So please provide us with code and stacktracke if possible – diazazar Jan 23 '14 at 13:56
  • DYM [`ProgressMonitor`](http://docs.oracle.com/javase/7/docs/api/javax/swing/ProgressMonitor.html)? The `JProgressBar` has no icon. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 23 '14 at 13:58
  • Hello mr paul.cioroianu , I know this is not a tutorial site... Currently I don't have any approch, that why I posted like that.. – user2914906 Jan 23 '14 at 14:13
  • If you want to **change** the icon of your progress bar, then you probably still have some code of something that shows you a progressbar with an icon, isn't it like that? There is neither an icon on a `JProgressBar` nor on a `ProgressMonitor`. So where did you find this icon which you want to change? It is probably no progress bar component! Tell us! – bobbel Jan 23 '14 at 14:25
  • hi bobbel, please tell me the way of changing icon see this link http://www.java2s.com/Code/Java/Swing-JFC/ProgressbarSample.htm there you can find an image of ProgressBar with java icon – user2914906 Jan 23 '14 at 14:28
  • So, now you have explained, which icon you mean! It's the icon of the `JFrame` class! See: http://stackoverflow.com/questions/15657569/how-to-set-icon-to-jframe – bobbel Jan 23 '14 at 14:33
  • Hi bobbel, there I don't have frame scope.... How can I set the icon?... – user2914906 Jan 23 '14 at 14:43
  • Then you have to add a parameter to the `SwingWorkerProgressMonitor` and/or the `BackingTask` constructor for a reference to your `JFrame`, set it to a member variable and access it with `setIconImage` in your desired method. – bobbel Jan 23 '14 at 14:55

1 Answers1

1

So, assuming you want to change the icon of your application (the JFrame) and you want to do it in a progress subclass, try the following way:

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(...);
    }
}
bobbel
  • 3,327
  • 2
  • 26
  • 43