GUI freezing java

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
This is my case:

I have a swing GUI with a jProgressBar on. Then I have a jFileChooser where you could choose images, which gets read by an input stream that searches the end of the files for some bytes.

I want the jProgressBar to update after every file gets searched, so if I choose 100 files, 1 file would be 1% complete.

Now I know how to do this, thing is that the GUI is frozen while it searches. How would I fix this? Threads?
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
This is my case:

I have a swing GUI with a jProgressBar on. Then I have a jFileChooser where you could choose images, which gets read by an input stream that searches the end of the files for some bytes.

I want the jProgressBar to update after every file gets searched, so if I choose 100 files, 1 file would be 1% complete.

Now I know how to do this, thing is that the GUI is frozen while it searches. How would I fix this? Threads?
Yes. You should probably use a thread to do the file searching, which has access to the JProgresBar. Look at:
http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html
(I would reccomend the first method used, since that means you can add the thread code within the same class where the code currently is.)
You then need to implement the whole searching code, which also updates the bar, within the run method of the thread, which you can then start using Thread.start(). Alternatively if the searching code is a separate method you could even just tell the run() method to simply call whatever method currently implements the searching code. Since the thread is run separately from the gui now, the gui stays responsive, even if a background task is going on.
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Yes. You should probably use a thread to do the file searching, which has access to the JProgresBar. Look at:
http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html
(I would reccomend the first method used, since that means you can add the thread code within the same class where the code currently is.)
You then need to implement the whole searching code, which also updates the bar, within the run method of the thread, which you can then start using Thread.start(). Alternatively if the searching code is a separate method you could even just tell the run() method to simply call whatever method currently implements the searching code. Since the thread is run separately from the gui now, the gui stays responsive, even if a background task is going on.

hmmm, I'm not doing it right :/

org.jvnet.substance.api.UiThreadingViolationException: Component state change must be done on Event Dispatch Thread
at org.jvnet.substance.utils.SubstanceCoreUtilities.testComponentStateChangeThreadingViolation(SubstanceCoreUtilities.java:2385)
at org.jvnet.substance.SubstanceProgressBarUI$1.stateChanged(SubstanceProgressBarUI.java:156)
at javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:348)
at javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:285)
at javax.swing.DefaultBoundedRangeModel.setValue(DefaultBoundedRangeModel.java:151)
at javax.swing.JProgressBar.setValue(JProgressBar.java:831)
at hidefiles.searchThread.run(searchThread.java:26)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-6" org.jvnet.substance.api.UiThreadingViolationException: Component state change must be done on Event Dispatch Thread
at org.jvnet.substance.utils.SubstanceCoreUtilities.testComponentStateChangeThreadingViolation(SubstanceCoreUtilities.java:2385)
at org.jvnet.substance.SubstanceProgressBarUI$1.stateChanged(SubstanceProgressBarUI.java:156)
at javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:348)
at javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:285)
at javax.swing.DefaultBoundedRangeModel.setValue(DefaultBoundedRangeModel.java:151)
at javax.swing.JProgressBar.setValue(JProgressBar.java:831)
at hidefiles.searchThread.run(searchThread.java:26)
at java.lang.Thread.run(Thread.java:619)

searchThread.java:
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hidefiles;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author vigge_sWe
 */
public class searchThread implements Runnable {

    public void run() {
        int l = Main.jFileChooser1.getSelectedFiles().length;
                for (int a = 0; a < l; a++) {

                        Main.jProgressBar1.setValue((a+1/l)*100);
                        File input = Main.jFileChooser1.getSelectedFiles()[a];
                        InputStream inFile = null;
                        //Initialise the value of 'valid' to false
                        String valid = "false";
                        //This is the signature every file should have
                        int[] filesigrar = {0x58, 0x59, 0x12, 0xA2};
                        int[] filesigzip = {0x58, 0x59, 0x12, 0xA3};
                        try {
                            inFile = new FileInputStream(input);
                            //Go to the last 4 bytes
                            inFile.skip(input.length() - 4);
                            //Compare each of the last 4 bytes to the expected signatures
                            for(int i=0; i<4; i++) {
                                int c = inFile.read();
                                if(c == filesigrar[i]) {
                                    valid = "RAR";
                                } else if(c == filesigzip[i]) {
                                    valid = "ZIP";
                                } else {
                                    //In case any of the 4 bytes fails the check, the file is invalid
                                    //and the loop is aborted.
                                    valid = "none";
                                    break;
                                }
                            }
                        //Any exception assumes that the input is invalid.
                        } catch (FileNotFoundException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                valid = "false";
                        } catch (IOException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                valid = "false";
                        }
                        Main.model.addRow(new Object[]{Main.jFileChooser1.getSelectedFiles()[a].getName(),
                                Main.jFileChooser1.getSelectedFiles()[a].length()/1024+" kb", valid});

                }
    }

}

Code:
Thread search = new Thread(new searchThread());
         search.start();
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Last edited:
Top