x
login about faq

Java swing background execution

Hi,

I am trying to make a progress monitor bar for a long execution. The program is basically trying to scan the local network (which takes about two minutes). I have a result class which prepares a JPanel that can be placed in the UI with the results. I want to have a neat progress bar for the whole process. I did make a ProgressMonitor object with a Task class and PropertyChangeListener as of the example in:

http://download.oracle.com/javase/tutorial/uiswing/components/progress.html

http://download.oracle.com/javase/tutorial/uiswing/examples/components/ProgressMonitorDemoProject/src/components/ProgressMonitorDemo.java

class Task extends SwingWorker<Void, Void> { @Override public Void doInBackground() {

        int progress = 0;
        setProgress(0);
        try {
            Thread.sleep(1000);
            while (progress < 100 && !isCancelled()) {

                Thread.sleep(10000);
                progress += 1;
                setProgress(progress);
            }
        } catch (InterruptedException ignore) {}
        return null;
    }

    @Override
    public void done() {
        Toolkit.getDefaultToolkit().beep();
                    progressMonitor.setProgress(0);
    }
}

so currently it is like a fake progress bar which just based on the thread sleep time. Currently my approach is create another thread in action listener class that runs the result class but for some reason I can't call back the JPanel object from asynchronously.... I tried to used the ExecutorService with FutureTask but not sure whether I am using it the right way.

A general flowchart of my current app is like

UI-->scan button-->at actionlistener for scan button creation of the swingworker for task and executorservice

public void actionPerformed(ActionEvent e) { if (e.getActionCommand ().equals ("Close")) { System.exit(0); }

    if (e.getActionCommand ().equals ("Scan"))
    {
       panel2.setVisible(false);
       panel2.removeAll();
       panel2.revalidate();









       progressMonitor = new ProgressMonitor(ui.this,
                "Running a scan",
                "", 0, 100);
          progressMonitor.setProgress(0);

          progressMonitor.setProgress(0);
               task = new Task();
               task.addPropertyChangeListener(this);
               task.execute();



              es = Executors.newFixedThreadPool(1);

||1*||------------------------>f = new FutureTask(new resultset()); es.submit(f);

    }


}

||1*|| is what i what to run parallel to the progress monitor

in the above code the scan button gets clicked and stays enabled until the scan is done but the progress monitor bar never shows up.

removing ||1*|| just shows up the progress bar but ofcourse the scan never took place.

ANY HELP IN CORRECT WAY OF SETTING IT UP or USE OF A DIFFERENT APPROACH WOULD BE GREATLY APPRECIATED!!

more ▼

asked Jul 19 at 04:58 PM

aybeeryu\'s gravatar image

Aybee Ryu
0 3 3

(comments are locked)
10|600 characters needed characters left

1 answer: sort voted first

yes, i gotcha boss! first and mostly go to Timer & TimerTask ( prob in util or something ) then do an un-sync() something or other on an [][] ( Raster.java ) obtained from a BufferedImage which is used as the object upon which graphics.draw() in paint operates

this no fun the first time and many times thereafter but getting the Timer & TimerTask to operate something like what you want is the first thing to do prior to doing any draw operation = the way you have it is ( short of testing to find out what it really does ) is likely to get you a form of Thread starvation

the the Timer & TimerTask keep you from that Drag_ON by design as they run in the JVM in a manner designed by engineers to avoid beginner style thread starvation

    class Blazitzo extends TimerTask {}//
Timer TimTwoTime = new Timer( new Blazitzo());//
TimTwoTime.start();

Nick

more ▼

answered Aug 12 at 08:55 PM

nicholas_jordan\'s gravatar image

nicholas_jordan
2 5

(comments are locked)
10|600 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

powered by AnswerHub - Enterprise Social Q&A