Progress Bars

LazzyIzzi got tired of writing the same JProgressBar code over and over

Home

A class to simplify creating and managing one or more progress bars in a single window.

The ProgressBars class wraps the methods of the JProgressBar class.

Progress bar window

Progress bars ImageJ plugin example.

  1. Download ProgressBars.jar and place it in the ImageJ plugins folder.
  2. Start or restart imageJ.
  3. In ImageJ, use Plugins>New>Plugin to create a new plugin text window.
  4. Select and delete the plugin window text.
  5. Copy the code below and paste it into the plugin text window.
  6. Save the plugin as Test_ProgressBars.java
  7. Select File>Compile and Run.
	
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JProgressBar;

import ij.IJ;
import ij.plugin.PlugIn;
import jhd.ProgressBars.*;

public class Test_ProgressBars implements PlugIn
{
	final Color myColor = new Color(240,230,190);//slightly darker than buff

	@Override
	public void run(String arg)
	{
		//ProgressBars(java.lang.String windowTitle)
		ProgressBars progBars = new ProgressBars("Progress Bars Example");
		
		//add(java.lang.String progBarTitle, int progressbarWidth, int progressbarHeight, int minBarValue, int maxBarValue)
		progBars.add("Main Process", 350, 20, 0, 1);
		progBars.add("Sub Process", 350, 20, 0, 1);			
		progBars.add("Wrap Up", 350, 20, 0, 1);
					
		progBars.setBackground(myColor);
		progBars.show();
		
		//public void setMaxBarValue(java.lang.String name, int value)
		progBars.setMaxBarValue("Main Process", 20);
		progBars.setMaxBarValue("Sub Process", 200);
		progBars.setMaxBarValue("Wrap Up", 200);
		
		//Example "get" calls, not required, values will appear in the ImageJ Log window 		
		ArrayList<JProgressBar> pbList = progBars.getProgBars();	
		for(JProgressBar pb : pbList)
		{
			IJ.log(pb.getName() + ", "+ pb.getMaximum());
		}
		
		JProgressBar[] pbArr = progBars.getProgBarsAsArray();	
		for(JProgressBar pb : pbArr)
		{
			IJ.log(pb.getName() + ", "+ pb.getMaximum());
		}
		
		// running the example processes (simple loops) and updating the appropriate progress bars
		//setValue(java.lang.String name, int value)
		for(int j=0;j<=20;j++)
		{
			for(int i=0;i<=200;i++)
			{
				progBars.setValue("Sub Process",i);
				IJ.wait(5);
			}
			progBars.setValue("Main Process",j);
		}

		for(int i=0;i<=200;i++)
		{
			progBars.setValue("Wrap Up",i);
			IJ.wait(5);
		}
		progBars.close();
	}
}

Progress bars application example.

  1. Download ProgressBars.jar to a location where it will be visible to your IDE.
  2. Create a new Java "main" project
  3. Create a class named something like progressBarsExample.
  4. Copy the code below and paste it into the progressBarsExample.java file
  5. Add ProgressBars.jar to the progressBarsExample project's build path.
  6. Run the executable.
	
import jhd.ProgressBars.*;
import java.awt.Color;
//import java.util.ArrayList;  //uncomment to use arraylst code
import javax.swing.JProgressBar;

public class testProgBars
{

	public static void main(String[] args)
	{
		final Color myColor = new Color(240,230,190);//slightly darker than buff
		ProgressBars progBars = new ProgressBars("Progress Bars Example");
		progBars.add("Main Process", 350, 20, 0, 1);
		progBars.add("Sub Process", 350, 20, 0, 1);			
		progBars.add("Wrap Up", 350, 20, 0, 1);			
		progBars.setBackground(myColor);
		progBars.show();
		
		progBars.setMaxBarValue("Main Process", 20);
		progBars.setMaxBarValue("Sub Process", 200);
		progBars.setMaxBarValue("Wrap Up", 200);
		
		//uncomment to use arraylst code		
//		ArrayList<JProgressBar> pbList = progBars.getProgBars();	
//		for(JProgressBar pb : pbList)
//		{
//			System.out.println(pb.getName() + ", "+ pb.getMaximum());
//		}
		
		JProgressBar[] pbArr = progBars.getProgBarsAsArray();	
		for(JProgressBar pb : pbArr)
		{
			System.out.println(pb.getName() + ", "+ pb.getMaximum());
		}

		for(int j=0;j<=20;j++)
		{
			for(int i=0;i<=200;i++)
			{
				progBars.setValue("Sub Process",i);
				try {Thread.sleep(5);}
				catch (InterruptedException e) { }
			}
			progBars.setValue("Main Process",j);
		}

		for(int i=0;i<=200;i++)
		{
			progBars.setValue("Wrap Up",i);
			try {Thread.sleep(5);}
			catch (InterruptedException e) { }
		}
		progBars.close();
	}

}

Home