5 Replies - 4185 Views - Last Post: 27 March 2011 - 09:47 PM Rate Topic: -----

#1 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Java Progress Bar

Posted 27 March 2011 - 07:09 PM

So I have come across a very interesting problem. I am doing my senior project in forensics and am using the "dd" command in Unix to convert raw NTFS image files.
The only problem is that I want to use a progress bar and I don't know how to properly integrate it into the programs interface. I have the following to convert the image.



import java.util.Stack;

/***
 * This class provides a mechanism for making system calls in a Unix shell environment. 
 * Example: initiate a bash script to convert the raw image file with an extension of *.dd
 * to a *.conv file with the same name using the Unix dd (data description) command.
 */
public class SystemCall implements Runnable {
	private Stack<String> cmdHistory;
	private String cmd;
	private Thread cmdThread;

	/**
	 * Default constructor initializes the command history stack.
	 */
	public SystemCall(){
		cmdHistory = new Stack<String>();
		cmdThread = new Thread(this); // pass the run method in from this (SystemCall) object.
	}
	
	/**
	 * @return The command to execute.
	 */
	public String getCmd(){
		return cmd;
	}
	
	/**
	 * @return The command history.
	 */
	public Stack<String> getCmdHistory() {
		return cmdHistory;
	}

	/**
	 * @return The thread which carries out the execution of the command.
	 */
	public Thread getCmdThread() {
		return cmdThread;
	}
	
	/*****
	 * The command thread carries out the execution of the command.
	 */
	public void execute(final String command){
		this.cmd = command;
		cmdThread.run(); // the command thread calls this.run().
	}
	
	/**
	 * The thread calling run hands the shell script off to the shell in order to execute properly.
	 * If the thread is interrupted by another thread an exception is thrown.
	 */
	public void run() {
		try {
			cmdHistory.push(cmd);
			// Use the -c option to read off each string in the cmd and execute it.
			while (Runtime.getRuntime().exec(new String []{"bash", "-c", cmd}).waitFor() != 0 /* wait for process to complete*/ ){
				System.out.println(Runtime.getRuntime().exec(new String []{"bash", "-c", cmd}).waitFor());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		cmdThread = null;
	}
	
	/***
	 * Returns the command history.
	 */
	public String getHistory(){
		return cmdHistory.toString();
	}
	
	/***
	 * Clears the history.
	 */
	public void clearHistory(){
		cmdHistory.clear();
	}
}



My progress bar is in my dry docks and has not been integrated into my system call yet. In other words the code below is stub which demonstrates
my understanding of the progress bar.

import javax.swing.*;

public class Progress extends JFrame {
    JProgressBar current;
    JTextArea out;
    JButton find;
    Thread runner;
    int num = 0;

    /******
     * Construct the progress bar.
     */
    public Progress() {
        super("Converting Raw Disk Image");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 100);
        current = new JProgressBar(0);
        current.setStringPainted(true);
        setContentPane(current);
    }
   
    public void iterate() {
        while (num < 120) {
        	if (num > 100)
                current.setIndeterminate(true);
            current.setValue(num);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) { }
            num += 3;
        }
        dispose(); // deallocate memory
    }

    public static void main(String[] arguments) {
        Progress frame = new Progress();
        frame.setVisible(true);
        frame.iterate();
   }
}



Any assistance would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Progress Bar

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Progress Bar

Posted 27 March 2011 - 07:15 PM

Not a good idea at all to mix thread and Swing
This is repeated and repeated and repeated in the documentation many and many times

Use a swing.Timer if you want to update GUI with a time based event
Was This Post Helpful? 1
  • +
  • -

#3 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: Java Progress Bar

Posted 27 March 2011 - 07:21 PM

View Postpbl, on 27 March 2011 - 07:15 PM, said:

Not a good idea at all to mix thread and Swing
This is repeated and repeated and repeated in the documentation many and many times

Use a swing.Timer if you want to update GUI with a time based event


Roger that. The getRuntime().exec() call is forking a process anyways I believe. So with the Timer should I just
determine the number of bytes that are in the current file and do some rough estimations? My biggest concern is
integrating the progress bar with the process that is taking care of the shell script.
Was This Post Helpful? 0
  • +
  • -

#4 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: Java Progress Bar

Posted 27 March 2011 - 07:27 PM

I feel a bit confused. I actually want the thread to cause a block until the raw image is converted through the Unix dd command. This happens when a user creates a new case. That user can not do anything until the passed in *.dd file is converted. So I don't see how Timer would help. I don't know what to do. I could set the indeterminate value to true on the progress bar, but this would prevent users from seeing the percentage of progress in the bar.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Progress Bar

Posted 27 March 2011 - 07:30 PM

Basically, if you are using Swing, do not implement Runnable
Use a Swing Timer for your asynchronous operations... that's it
unless you really know what is happen behind which took me 7 years to understand with the help of Dogstopper and macosxnerd101
and we had a lot of deep philosophical discussions about it, we can actually write a book about it
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Java Progress Bar

Posted 27 March 2011 - 09:47 PM

If you want to read in your image concurrently, then perhaps a SwingWorker is a better tool. It works as you would want a Thread to, but without having to worry about Thread-safety with Swing. Or what you could do is the following:
-Pass the Image to a Method to convert it
-On certain milestones in the conversion, have the method update the BoundedRangeModel's value of the JProgressBar. This will update the display.
-Before the method terminates, set the BoundedRangeModel's value to 100%.

If you want to queue multiple conversion tasks, then we can tweak the design some. But this will do one conversion before allowing the user to enter another.

Also, check out my blog entry on the Java GUI for more on why Swing isn't Thread-safe, as well as some alternatives.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1