Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Progress Bar Thread Java

  • 02-07-2009 4:40pm
    #1
    Registered Users, Registered Users 2 Posts: 227 ✭✭


    I am trying to get a progress bar going so that it shows the app is doing something while I am processing a file in the background. I have had a read up on it and according to the net the easiest way to do it is to create a thread which will run the progress bar. I am fine with all this but when I run my code the progress bar appears but doesn't update, I have put a println in the code for the progress bar and it appears to process the code for it alright but for some reason it won't update the progress bar. When the file is finished processing the progress bar updates to 100% in one go. Anyone got any ideas??


Comments

  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    Post the code up, make it easier to debug.


  • Registered Users, Registered Users 2 Posts: 427 ✭✭Kevo


    Did you call jpanel.updateUI()?


  • Registered Users, Registered Users 2 Posts: 4,287 ✭✭✭NotMe


    Are you using JProgressBar?


  • Registered Users, Registered Users 2 Posts: 227 ✭✭rebellad


    Here is the code guys :

    Calling code :

    ClsProgressMonitor t = new ClsProgressMonitor();
    Thread th = new Thread(t);
    th.start();


    ClsProgressMonitor

    package ie.xxx.retail.YYYYY;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ClsProgressMonitor implements Runnable {

    static JProgressBar jpb;
    static ClsProgressMonitor _this;

    public ClsProgressMonitor (){
    _this = this;
    }

    public static void createAndShowGUI(){
    JFrame aFrame = new JFrame("Swing Thread Example: Fixed Threading");
    JButton aButton = new JButton("Do Something");
    //aFrame.getContentPane().add(aButton, BorderLayout.SOUTH);
    //aButton.addActionListener(_this);
    JPanel aPanel = new JPanel();
    aPanel.add(new JLabel("Progress:"));
    jpb = new JProgressBar(0,100);
    aPanel.add(jpb);
    aFrame.getContentPane().add(aPanel);
    aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aFrame.pack();
    aFrame.setVisible(true);

    }



    // worker thread
    public void run(){
    createAndShowGUI();
    while(true){
    // wait for the signal from the GUI
    //try{synchronized(this){wait();}}
    // catch (InterruptedException e){}
    // simulate some long-running process like parsing a large file
    for (int i = 0; i <= 100; i++){
    jpb.setValue(i);
    jpb.updateUI();
    System.out.println("actionPerformed sets jpb value to: "+i);
    try{Thread.sleep(50);} // make the process last a while
    catch (InterruptedException e){}
    }
    break;
    }
    }
    }


  • Closed Accounts Posts: 7 itscout


    When running your example the progress bar updates fine, incrementally, not all in one go - isn't the same for you with this exact code snippet ?

    In this snippet you simulate I/O by doing
    rebellad wrote: »
    Here is the code guys :
    for (int i = 0; i <= 100; i++){
    .....
    try{Thread.sleep(50);} // make the process last a while
    catch (InterruptedException e){}
    }
    .....
    }


    Is your real code splitting the I/O operations in 100 roughly equivalent sub-operations and after each one you call "jpb.setValue(i); " ?

    Maybe this snippet doesn't closely replicate your real usecase ?
    In this code I see no reason for a separate thread. The "IO" and GUI updates are happening in the same thread
    --
    IT Scout - www.jobscout.ie


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 227 ✭✭rebellad


    Thanks ITScout, yeah if I run that code on it's own there is no problem. But in my app code what I am doing is reading info from a file in another class so I call

    ClsProgressMonitor t = new ClsProgressMonitor();
    Thread th = new Thread(t);
    th.start();

    to run the progress bar while the main app reads in the file. It is really strange because no matter what I do the progress bar will not update until the main app is finished processing. It shows the blank jframe that the progress bar is on but the progress bar doesn't show until the main app is finished


  • Closed Accounts Posts: 7 itscout


    rebellad wrote: »
    Thanks ITScout, yeah if I run that code on it's own there is no problem. But in my app code what I am doing is reading info from a file in another class so I call

    ClsProgressMonitor t = new ClsProgressMonitor();
    Thread th = new Thread(t);
    th.start();

    to run the progress bar while the main app reads in the file.

    I'm afraid it's hard to help without the actual code. It would be much easier to first be able to replicate your problem and then figure it out.

    The main question is which thread and when triggers the progress bar update in your real code.
    --
    IT Scout - www.jobscout.ie


  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    Try calling repaint () at the end of run ()?

    See what happens if you don't use the sleep, also.


  • Closed Accounts Posts: 198 ✭✭sh_o


    It has been a long time since I touched swing, but iirc there is the swingutilities class which handle some of the threads in a swing gui. google for concurrency and swing or invokeLater and that may help.


Advertisement