Swing: does DefaultBoundedRangeModel coalesce multiple events?

Posted by Jason S on Stack Overflow See other posts from Stack Overflow or by Jason S
Published on 2010-06-16T20:07:51Z Indexed on 2010/06/16 20:12 UTC
Read the original article Hit count: 211

Filed under:
|
|

I have a JProgressBar displaying a BoundedRangeModel which is extremely fine grained and I was concerned that updating it too often would slow down my computer. So I wrote a quick test program (see below) which has a 10Hz timer but each timer tick makes 10,000 calls to microtick() which in turn increments the BoundedRangeModel. Yet it seems to play nicely with a JProgressBar; my CPU is not working hard to run the program.

How does JProgressBar or DefaultBoundedRangeModel do this? They seem to be smart about how much work it does to update the JProgressBar, so that as a user I don't have to worry about updating the BoundedRangeModel's value.

package com.example.test.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class BoundedRangeModelTest1 extends JFrame {
    final private BoundedRangeModel brm = new DefaultBoundedRangeModel();
    final private Timer timer = new Timer(100, new ActionListener()
    {
        @Override public void actionPerformed(ActionEvent arg0) { tick(); }
    });
    public BoundedRangeModelTest1(String title) {
        super(title);

        JPanel p = new JPanel();

        p.add(new JProgressBar(this.brm));
        getContentPane().add(p);

        this.brm.setMaximum(1000000);
        this.brm.setMinimum(0);
        this.brm.setValue(0);       
    }

    protected void tick() {
        for (int i = 0; i < 10000; ++i)
        {
            microtick();
        }
    }   
    private void microtick() {
        this.brm.setValue(this.brm.getValue()+1);
    }

    public void start() 
    {
        this.timer.start();
    }

    static public void main(String[] args)
    {
        BoundedRangeModelTest1  f = 
            new BoundedRangeModelTest1("BoundedRangeModelTest1");

        f.pack();
        f.setVisible(true);     
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);

        f.start();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing