2

I have the following Problem:

I have a JTable which shows me the progress of a command. I have 2 sort of commands. Determined and indetermined commands.

If i only have indetermined progressbars in the table the progress is shown correct. but if i have one determined progressbars in it, the other indetermined wont be animated.

Here is my code:

class ProgressBarRenderer implements TableCellRenderer {

    /** The bar. */
    private JProgressBar bar = new JProgressBar() {
        public boolean isDisplayable() {
            return true;
        };
    };

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        ProgressBarInfo pbi = (ProgressBarInfo) value;
        if (pbi.getType() == Progress.LS) {
            if (pbi.getValue() == -1) {
                bar.setIndeterminate(false);
                bar.setValue(0);
                bar.setString("Progress ended");
            } else {
                bar.setIndeterminate(true);
                bar.setString(pbi.getValue() + " " + pbi.getText());
            }
            bar.setStringPainted(true);

            return bar;
        } else if (pbi.getType() == Progress.SCP) {
            if (pbi.getValue() == -1) {
                bar.setIndeterminate(false);
                bar.setValue(0);
                bar.setString("Progress ended");
            } else {
                bar.setValue(pbi.getValue());
                bar.setString(pbi.getValue() + "% " + pbi.getText());
            }
            bar.setIndeterminate(false);
            bar.setStringPainted(true);
        }
        return bar;
    }
}

The Table is updated every 10 milliseconds by a swingworker thread, so don't mind about that.

NotANormalNerd
  • 415
  • 3
  • 14

2 Answers2

5

The internal state of the progressBar (determinate vs. indeterminate) is very different - switching between them on the same instance might be the reason. Try to use one instance for each

public class MyRenderer ....
    JProgressBar determinate;
    JProgressBar indeterminate; 

    public Component getTableCellRendererComponent(...) {
         if (value.isDeterminate) {
           ...
           return determinate;
         }
         ....
         return undeterminate
    } 

Edit

checked: it is working, though might look a bit weird with many indeterminate cells which are animated "in step". Just beware: the internal - in the ui delegate - animation state is undocumented, so there might be LAFs where it doesn't work at all (f.i. Substance, afair).

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 This would be good start at re-factoring. The original is too convoluted, asymmetric and redundant to debug by inspection, at least for me. – trashgod Mar 01 '12 at 11:26
  • Thank you that did it perfectly... never would have thought of that... great Thank you again. – NotANormalNerd Mar 01 '12 at 12:35
  • [please are you know about ???](http://stackoverflow.com/questions/5898028/what-happened-to-the-substance-laf), not possible to undelete this post, how I hate meta_SO, otherwise I sticked my request about undelete there – mKorbel Apr 18 '13 at 13:47
  • @mKorbel no idea why that is deleted ... the only option is indeed to go to meta, always fun there ;-) – kleopatra Apr 18 '13 at 14:29
  • @kleopatra :-) eeeee [lets this decision for fun_factory](http://meta.stackexchange.com/questions/177092/request-undelete-quite-valuable-thread-on-so) – mKorbel Apr 18 '13 at 15:32
  • @kleopatra eeeerght as we said lots of fun, doomed to disgrace and moodiness of this community, match drawn 5:5 – mKorbel Apr 18 '13 at 16:15
  • @mKorbel thanks for the link, upvoted and commented - not much else we can do, I'm afraid .. – kleopatra Apr 18 '13 at 16:35
3

probably you have an issue with Concurency in Swing, basically there are two ways

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319