svkvvenky
svkvvenky

Reputation: 1120

Displaying a custom progress string on a JProgressBar

When setStringPainted() of JProgressBar is used,the amount of process completion is displayed in terms of percentage.

But how can i customise setStringPainted() so that i can display remaining time instead of percentage?

Upvotes: 2

Views: 4627

Answers (2)

kleopatra
kleopatra

Reputation: 51535

Interestingly (read: I'm astonished :-) you'll have to implement any value-dependent progress string yourself, by overriding getString

    final JProgressBar bar = new JProgressBar() {

        @Override
        public String getString() {
            int max = getMaximum();
            return super.getString() + (max - getValue());
        }

    };
    bar.setStringPainted(true);
    ActionListener l = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            bar.setValue(bar.getValue() + 1);
        }
    };
    bar.setString("missing: ");
    new Timer(500, l).start();

Upvotes: 4

Joey
Joey

Reputation: 354744

setString() sets the progress string. If that property is null then only a simple percentage is shown. This is pointed out clearly in the documentation:

setString

public void setString(String s)

Sets the value of the progress string. By default, this string is null, implying the built-in behavior of using a simple percent string. If you have provided a custom progress string and want to revert to the built-in behavior, set the string back to null.

The progress string is painted only if the isStringPainted method returns true.

Parameters:

s – the value of the progress string

Upvotes: 4

Related Questions