Avatar of Member_2_2394978
Member_2_2394978Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Progress bar and frame not showing until complete!

Hi Experts,

I've been using many tutorials and so far haven't been able to get my progress bar working correctly.  At current I have the attached class (minus a couple of non important methods).

This class is then declared as :
     protected DataProcessing process = null;
and used with the following two lines of code:
     process = new DataProcessing(header.iNeuron);
     process.getPeaks(afNorm, fMinValue, fMaxValueAbsolute, afPeaks, abPos);

Yet even though I have the frame to open up and the processing to be in a background thread which I understand is important because if not the GUI can not be updated with the current progress.  Even though, the frame, along with the progress bar are not showing until the progress is complete!

Any suggestions?
Thanks
James
package Data;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
 
 
 
public class DataProcessing extends JFrame implements Serializable, PropertyChangeListener
{
    private JProgressBar progressBar = null;
    private GridBagConstraints m_c = new GridBagConstraints();
    private JPanel m_optionPanel = new JPanel();
    private Task task = null;
    
    private float[][][] afNorm = null;
    private float fMin = 0.0f;
    private float fMax = 0.0f;
    private float[][] afPeaksOut = null;
    private int[][]abPosOut = null;
    
    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            setProgress(0);
            float fPeak;
            for (int x = 0; x < afNorm.length; x++) {
                for (int y = 0; y < afNorm[x].length; y++)
                {
                    fPeak = 0.0f;
                    for (int z = 0; z < afNorm[x][y].length; z++) {
                        if (afNorm[x][y][z] >= fMin && afNorm[x][y][z] <= fMax){
                            if (fPeak < afNorm[x][y][z]) {
                                fPeak = afNorm[x][y][z];
                                abPosOut[x][y] = z;
                            }
                        }
                    }
                    afPeaksOut[x][y] = fPeak;
                }
                setProgress(x);
            }
            return null;
        }
 
        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            // nothing!
        }
    }  
    
        /**
     * Create the GUI and show it. As with all GUI code, this must run
     * on the event-dispatching thread.
     */
    private void createAndShowGUI(int maximum) {  
        //setup the look and feel to the local computer
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);}
        catch (Exception e) {System.err.println("L&F Error: " + e);}
        
        this.setBounds(400, 400, 0, 0);
        this.setPreferredSize(new Dimension(300, 100));
        this.setResizable(false);
        
        // PROGRESS BAR
        progressBar = new JProgressBar(0, maximum);
        progressBar.setValue(0);
        
        // setup layout
        m_optionPanel.setLayout(new GridBagLayout());
        
        // add components to the pane!
        m_c.anchor = GridBagConstraints.LINE_START;
        m_c.weighty = 0.0;
        // DISPLAY
        m_c.gridx = 0;
        m_c.gridy = 0;
        m_c.gridwidth = 20;
        m_optionPanel.add(progressBar, m_c); 
        
        // Setup the JFrame
        setContentPane(m_optionPanel);
        setBackground(Color.white);
        setTitle("Processing ...");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
 
        pack();
        System.out.println("created");
    }
    
    public DataProcessing(final int maximum) {      
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(maximum);
            }
        });
    }
    
    /*Self Contained function to derive peaks in window from all correls*/
    public void getPeaks(float[][][] afNormIn, float fMinIn, float fMaxIn,
            float[][] afPeaksOutIn, int[][] abPosOutIn)
    {
        afNorm = afNormIn;
        fMin = fMinIn;
        fMax = fMaxIn;
        afPeaksOut = afPeaksOutIn;
        abPosOut = abPosOutIn;
        
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }
 
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        }
    }
}

Open in new window

Java

Avatar of undefined
Last Comment
CEHJ
Avatar of softwarepearls_com
softwarepearls_com

You're not showing how the setProgress(int) method is implemented. The problem may be in that method..
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

There's not much wrong with it that i can see. I've hacked it so it will run as an app (see attached)
DataProcessing.java.txt
Avatar of Member_2_2394978

ASKER

I haven't got a setProgress() method, it seems to be inherint in the SwingWorker.  

Thanks CEHJ although I want this to be an object of an app; i'll try moving the code you have placed in void main ... to the void main of the app creating the DataProcessing object and see if that works :)
Avatar of Member_2_2394978

ASKER

Ok I now have it appearing at the beggning!
Although it doesn't update through the process!

It seems that the change listener isn't being called, because I put a println in there and it doesn't print!  
Any ideas?
package Data;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
 
 
 
public class DataProcessing extends JFrame implements Serializable, PropertyChangeListener
{
    private JProgressBar progressBar = null;
    private GridBagConstraints m_c = new GridBagConstraints();
    private JPanel m_optionPanel = new JPanel();
    private Task task = null;
    
    private float[][][] afNorm = null;
    private float fMin = 0.0f;
    private float fMax = 0.0f;
    private float[][] afPeaksOut = null;
    private int[][]abPosOut = null;
    
    private int m_maximum = 100;
    
    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            setProgress(0);
            float fPeak;
            for (int x = 0; x < afNorm.length; x++) {
                for (int y = 0; y < afNorm[x].length; y++)
                {
                    fPeak = 0.0f;
                    for (int z = 0; z < afNorm[x][y].length; z++) {
                        if (afNorm[x][y][z] >= fMin && afNorm[x][y][z] <= fMax){
                            if (fPeak < afNorm[x][y][z]) {
                                fPeak = afNorm[x][y][z];
                                abPosOut[x][y] = z;
                            }
                        }
                    }
                    afPeaksOut[x][y] = fPeak;
                }
                setProgress(x);
            }
            return null;
        }
 
        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            // nothing!
        }
    }  
    
        /**
     * Create the GUI and show it. As with all GUI code, this must run
     * on the event-dispatching thread.
     */
    public void createAndShowGUI() {  
        //setup the look and feel to the local computer
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);}
        catch (Exception e) {System.err.println("L&F Error: " + e);}
        
        this.setBounds(400, 400, 0, 0);
        this.setPreferredSize(new Dimension(300, 100));
        this.setResizable(false);
        
        // PROGRESS BAR
        progressBar = new JProgressBar(0, m_maximum);
        progressBar.setValue(0);
        
        // setup layout
        m_optionPanel.setLayout(new GridBagLayout());
        
        // add components to the pane!
        m_c.anchor = GridBagConstraints.LINE_START;
        m_c.weighty = 0.0;
        // DISPLAY
        m_c.gridx = 0;
        m_c.gridy = 0;
        m_c.gridwidth = 20;
        m_optionPanel.add(progressBar, m_c); 
        
        // Setup the JFrame
        setContentPane(m_optionPanel);
        setBackground(Color.white);
        setTitle("Processing ...");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
 
        pack();
    }
    
    public DataProcessing() { }
    
    public void setMaximum(int maximum) {
        progressBar.setMaximum(maximum);
    }
 
    public void propertyChange(PropertyChangeEvent evt) {
        System.out.println("changed");
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        }
    }
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

With something like this, i'll need runnable code please
Avatar of Member_2_2394978

ASKER

(Increased points, for your time)

I've zipped up the code and a data file, if thats ok.

I'm now getting a NullPointerException for the propertyChange method of the ProgressBar.
I think theh updating is perhaps because my code is still running on the event dispatch thread so its not updating the progress bar!

Thanks
Avatar of Member_2_2394978

ASKER

Oops, forgot file! Had to rename them all to bmp - probably not the best way thinking about it now!
Data file is the "100 ..."
Summer.zip
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Member_2_2394978

ASKER

Ah ok, like a main frame with gui with buttons on - i shall do that later, makes sense.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Member_2_2394978

ASKER

Ok cool.  I've seperated and done that, although progress bar still don't work :(
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

If you post your latest i'll try to look at it later
Avatar of Member_2_2394978

ASKER

Hmm - Ok i got it going now its seperated - I wasn't running the thread properly - although now I want one thread to wait, and the other to notify when finished!

i've put in

this.wait(); - for main programme extending JFrame
and in the done() method of the SwingWorker notifyAll();

Although they through exceptions, I looked the exception up and it needs to be locked or synchronized or something, although I don't quite understand them! -

synchronized(whats here) {
     wait();
}

Thanks
James
Avatar of Member_2_2394978

ASKER

Ok no worries, I sorted it - Thanks

I think the main problem was that I was getting confused with the threads etc. because I hadn't split the code up!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo