🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8937

ProgressBar doesn't update until FOR loop ends?

#8937
From: Matt Stevens
Date: Tue, 13 Jun 2000 00:00
248 lines
7117 bytes
Hi all,

I can not seem to update a  progressBar in Real-Time.  I wrote a simple
app (below) to show the problem.  I put a PB.setValue() inside a FOR
loop, the the PB doesn't update until the FOR loop finishes.  Does
anyone know what the problems is?

Matt
---------------------------------------
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.control.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import jclass.bwt.JCProgressMeter;

//=============================================
public class progressApp extends JFrame
{
  ButtonControl buttonControl1 = new ButtonControl();
  XYLayout xYLayout1 = new XYLayout();
  ButtonControl buttonControl2 = new ButtonControl();
  //JProgressBar jProgressBar1 = new JProgressBar();
  JCProgressMeter jProgressBar1 = new JCProgressMeter();
  JButton jButton1 = new JButton();
  progressDLG pd = new progressDLG(this,"Test Progress Bar DLG",false);
  JMenuBar menuBar1 = new JMenuBar();
  JMenu menu1 = new JMenu();
  JMenuItem menuItem1 = new JMenuItem();


  //Main method ---------------------------------------------
  public static void main(String[] args) {
    try  {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
    }
    new progressApp();
  }

  //Construct the application -----------------------------
  public progressApp()
  {
    //enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    pack();
    validate();
    this.setVisible(true);
  }

   //Component initialization -------------------------
  private void jbInit() throws Exception  {
    buttonControl1.setLabel("showDLG");
    buttonControl1.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        buttonControl1_mouseClicked(e);
      }
    });
    this.getContentPane().setLayout(xYLayout1);
    this.setJMenuBar(menuBar1);
    this.setTitle("Test  Prog Bar");
    xYLayout1.setHeight(200);
    xYLayout1.setWidth(300);
    this.setSize(300,200);
    this.setLocation(200,200);
    buttonControl2.setLabel("start pBar");
    buttonControl2.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        buttonControl2_mouseClicked(e);
      }
    });
    jButton1.setText("start");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        jButton1_mouseClicked(e);
      }
    });
    menu1.setLabel("File");
    menuItem1.setLabel("exit");
    menuItem1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        menuItem1_actionPerformed(e);
      }
    });
    this.getContentPane().add(jProgressBar1, new XYConstraints(11, 41,
226, 34));
    this.getContentPane().add(buttonControl1, new XYConstraints(8, 97,
77, -1));
    this.getContentPane().add(jButton1, new XYConstraints(104, 98, 73,
-1));
    this.getContentPane().add(buttonControl2, new XYConstraints(194, 99,
-1, -1));
    menuBar1.add(menu1);
    menu1.add(menuItem1);
  }



  void buttonControl1_mouseClicked( MouseEvent e )
  {
     //System.out.println("Show the Dialog");
     pd.setValue(0);
     pd.setVisible(true);
     if (pd.isVisible())
     {
             //System.out.println("PD dlg is visible");
     } else {
              //System.out.println("PD dlg is not visible");
     }
  }



  //----------------------------------------------
  void buttonControl2_mouseClicked(MouseEvent e) {

     try {
         for (int i = 0; i < 101; i++)
         {
            System.out.println("In JProgress Bar FOR loop I = "+i);
            Thread.sleep(100);
            jProgressBar1.setValue(i);
            //jProgressBar1.repaint();
         }
     } catch (Throwable t) { }
  }

  //--------------------------------------
  void jButton1_mouseClicked(MouseEvent e)
  {
    try {
         for (int i = 0; i < 101; i++)
         {
            System.out.println("In DLG FOR loop I = "+i);
            Thread.sleep(100);
            pd.setValue(i);
         }
     } catch (Throwable t) { }
     //pd.setVisible(false);

  }

  void menuItem1_actionPerformed(ActionEvent e) {
      System.exit(0);
  }
}
//-----------------------------

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;

public class progressDLG extends JDialog {
  JPanel panel1 = new JPanel();
  XYLayout xYLayout1 = new XYLayout();
  JProgressBar jProgressBar1 = new JProgressBar();
  public int totFonts = 0;
  public int curFonts = 0;
  public int col = 100;
  public int row = 100;

  public progressDLG(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    try  {
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  public progressDLG() {
    this(null, "Adding Fonts...", false);
  }

  void jbInit() throws Exception {
    jProgressBar1.setValue(0);
    panel1.setLayout(xYLayout1);
    xYLayout1.setHeight(93);
    xYLayout1.setWidth(452);
    getContentPane().add(panel1);
    panel1.add(jProgressBar1, new XYConstraints(12, 34, 421, 29));
    this.setLocation(col,row);
  }
  public void setValue( int n ) {
         jProgressBar1.setValue( n );
  }

  public void incrValue() {
         curFonts++;
         int p = (curFonts * 100) / totFonts;
         jProgressBar1.setValue(p);
         jProgressBar1.repaint();
  }

  public void setNumFonts( int n ) {
       totFonts = n;
  }


  public void setULHC( Point p ) {
     this.setLocation( p );
  }



  public static void main() {
     JFrame fr = new JFrame("test");
     progressDLG pd = new progressDLG(fr,"Wait",false);
     pd.setValue(0);
     fr.setVisible(true);
     pd.setVisible(true);
     try {
            for (int i = 0; i < 101; i++) {
               Thread.sleep(10000);
               pd.setValue(i);
            }
          }
            catch (Throwable t) {}
  }

}


--
__________________________________
Dr. Matt H. Stevens
GUI Development Specialist
3M Company
3M Center, Bldg 235-2AWS-63
Saint Paul, MN  55144-1000  USA

TEL:   651-736-1344
FAX:   651-737-0410
email: mhstevens@mmm.com
__________________________________
Technical Details
Message-ID: <394673A9.F203FA29@mmm.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,502ec09032bb7e80,start
X-Google-Attributes: gid111d45,public
From: Matt Stevens <mhstevens@mmm.com>
Subject: ProgressBar doesn't update until FOR loop ends?
Date: 2000/06/13
Message-ID: <394673A9.F203FA29@mmm.com>
X-Deja-AN: 634063502
Content-Transfer-Encoding: 7bit
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: 13 Jun 2000 10:39:41 -0800, 130.99.236.32
Organization: 3M Company
MIME-Version: 1.0
Reply-To: mhstevens@mmm.com
Newsgroups: borland.public.jbuilder.java.swing