Article View: borland.public.jbuilder.java.swing
Article #8938Re: ProgressBar doesn't update until FOR loop ends?
From: Jeroen Zwartepoorte
Date: Tue, 13 Jun 2000 00:00
Date: Tue, 13 Jun 2000 00:00
263 lines
7974 bytes
7974 bytes
The GUI is not updated during the FOR loop. That's why the progressbar is
either empty or full. You need to perform your for loop in a separate
thread. Read about the javax.swing.JProgressBar control for some additional
info.
Jeroen
"Matt Stevens" <mhstevens@mmm.com> wrote in message
news:394673A9.F203FA29@mmm.com...
> 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;
>
> file://=============================================
> public class progressApp extends JFrame
> {
> ButtonControl buttonControl1 = new ButtonControl();
> XYLayout xYLayout1 = new XYLayout();
> ButtonControl buttonControl2 = new ButtonControl();
> file://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();
>
>
> file://Main method ---------------------------------------------
> public static void main(String[] args) {
> try {
>
> UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
> }
> catch(Exception e) {
> }
> new progressApp();
> }
>
> file://Construct the application -----------------------------
> public progressApp()
> {
> file://enableEvents(AWTEvent.WINDOW_EVENT_MASK);
> try {
> jbInit();
> }
> catch(Exception e) {
> e.printStackTrace();
> }
> pack();
> validate();
> this.setVisible(true);
> }
>
> file://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 )
> {
> file://System.out.println("Show the Dialog");
> pd.setValue(0);
> pd.setVisible(true);
> if (pd.isVisible())
> {
> file://System.out.println("PD dlg is visible");
> } else {
> file://System.out.println("PD dlg is not visible");
> }
> }
>
>
>
> file://----------------------------------------------
> 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);
> file://jProgressBar1.repaint();
> }
> } catch (Throwable t) { }
> }
>
> file://--------------------------------------
> 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) { }
> file://pd.setVisible(false);
>
> }
>
> void menuItem1_actionPerformed(ActionEvent e) {
> System.exit(0);
> }
> }
>
>
>
> file://-----------------------------
>
> 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:
<8i64er$8h82@bornews.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<394673A9.F203FA29@mmm.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,502ec09032bb7e80 X-Google-Attributes: gid111d45,public From: "Jeroen Zwartepoorte" <Jeroen@xs4all.nl> Subject: Re: ProgressBar doesn't update until FOR loop ends? Date: 2000/06/13 Message-ID: <8i64er$8h82@bornews.borland.com> X-Deja-AN: 634124509 References: <394673A9.F203FA29@mmm.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.swing