Refresh poblem with JProgressBar

Trying to make a JProgressBar work, problem is that it does not display the value of progress until code has completed.  its setting the value but becuase code is still running it wont update until the last line of code is exectued. is there a way to refresh the screen of the main form to reflect the actual value of the progress bar, thanks

method in main form:

    public void setProgressStatus(int value, String action){
        progVal = progVal + value;
        lblProgress.setText(action);
        progressBar.setValue(progVal);
        if (value >1000){
            Toolkit.getDefaultToolkit().beep();
            timer.stop();
            //startButton.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            progressBar.setValue(progressBar.getMinimum());            
        }
        this.repaint();
    }

code from another class (has reference to main form)

         psForm.setProgressStatus(10, "deleteing table");
davidnewmanukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JoshWegenerCommented:
What type of window is "this" in? what is the window name and type?
devildog1775Commented:
Insure that you are calling validate() on the main form
JoshWegenerCommented:
You might need to do a window.pack();  // window is what/ever your window is called
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

JoshWegenerCommented:
Make sure you are refreshing all of your Containers / panels
devildog1775Commented:
Also insure that you operation is not blocking the ActionEvent thread. Blocking this thread will make your GUI appear to freeze
davidnewmanukAuthor Commented:
ive tried calling validate and did not work, dont if theres anywhere where i can call window.pack.

how do you refresh a panel  i've tried panel.repaint(); but that dont work???
devildog1775Commented:
Call pack() on whatever JFrame your JPanel is in
JoshWegenerCommented:
JPanel you can do a .repaint() - JFrame you do a .pack()
JoshWegenerCommented:
Can you post your code in here??
davidnewmanukAuthor Commented:
heres the relevent parts


public class PrioritySettingsForm extends JPanel implements ActionListener{
     public PrioritySettingsForm() {
             lblProgress = new JLabel("Progress Actions");
             pane.add(lblProgress);
             ......
     }

    public void setProgressStatus(int value, String action){
        progVal = progVal + value;
        lblProgress.setText(action);
        progressBar.setValue(progVal);
        System.out.println(progVal);
        if (value >1000){
            Toolkit.getDefaultToolkit().beep();
            timer.stop();
            //startButton.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            progressBar.setValue(progressBar.getMinimum());            
        }

    }


  private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("ListSelectionDemo");
        //frame.setPreferredSize(new Dimension(1028, 760));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PrioritySettingsForm demo = new PrioritySettingsForm();
        demo.setOpaque(true);
        frame.setContentPane(demo);
        frame.pack();
        frame.setVisible(true);
    }
 
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        try {
            if (source == cmdApplyChanges){
             pList = new PriorityList(this);

                pList.applyPriorities();
                JOptionPane.showMessageDialog(this, "Finsihed");
            }
        }catch(Exception er){
            er.printStackTrace();
        }
    }
   
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


class prioritylist:
    public PriorityList(PrioritySettingsForm inForm) {
        man = ConnectionManager.getInstance();
        con = man.getConnection();
        try{
            psForm = inForm;
        }catch(Exception e){
            e.printStackTrace();
        }    
    }
    public void applyPriorities() throws SQLException { //tbl_OrderPriority holds scoring info
        deleteOldPriorities();
        psForm.setProgressStatus(10, "Deleting Old Priorities");
        for (Priority p: priorities){ //delete first
            psForm.setProgressStatus(10, p.getPName());
            psForm.validate();
            runalogritm()
        }
    }
devildog1775Commented:
This method
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        try {
            if (source == cmdApplyChanges){
             pList = new PriorityList(this);

                pList.applyPriorities();
                JOptionPane.showMessageDialog(this, "Finsihed");
            }
        }catch(Exception er){
            er.printStackTrace();
        }
    }

is blocking the EventThread you should spawn this work off in another Thread using the SwingWorker.invokeLater( Runnable thread) method
JoshWegenerCommented:
Make this part global..

JFrame frame = new JFrame("ListSelectionDemo");

then do a frame.pack(); in the "setProgressStatus"
JoshWegenerCommented:
"devildog1775" is right, if you open a new thread, it will allow you to do more then one thing at a time..

Here is an tutoral on how to use threads:
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
davidnewmanukAuthor Commented:
when i declare as global JFrame frame; at top of class becuase createAndShowGUI is static i get the following error:

non-static variable cannotbe reference from a static context

on code:

frame = new JFrame("ListSelectionDemo");
devildog1775Commented:
e.g. change PriorityList to implement Runable and in your actionPerformed method do
            if (source == cmdApplyChanges){
                    SwingWorker.invokeLater( new PriorityList(this)) );
            }

change the applyPriorities to implement the run method of Runnable and in there do
 pList.applyPriorities();
 JOptionPane.showMessageDialog(this, "Finsihed");



JoshWegenerCommented:
Here is a better link ( has all 3 pages in 1 )

http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads_p.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davidnewmanukAuthor Commented:
the main class extends a jframe

the sub class with algorithm extends AbstractTableModel

so will this still work becuase i need to use the subclass as the model for the table first and then use the other methods for running algorithm
devildog1775Commented:
Whatever work that you need to do that requires extensive processing as it appears that PriorityList does needs to be done in a separate thread to avoid blocking the EventThred.  It does not matter how this Runnable is implemented, ie it can be a private class, just have it do its work and report back its status.
davidnewmanukAuthor Commented:
which of the classes will implement runnable??
davidnewmanukAuthor Commented:
thanks alot guys, got it sorted at last!
devildog1775Commented:
Thanks "davidnewmanuk"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.