Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Progress Bar Problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    USA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Progress Bar Problem

    Hey Guys,

    I've been working on a networked application in Java for the past couple of weeks. But I've hit the same stumbling block I always seem to hit when working on larger applications and it comes down to how to effectively implement a progress bar. I can never seem to get those things down.

    Basically the part I'm stuck on here is the login to the application. Basically the application on the client computer accesses files stored on the server computer. I've got all the networking down, as well as the security of the connection between the two machines (hybrid algorithm) but I am trying to implement a progress bar in the login process.

    The structure of the program with regards to this part of it works like this:

    - There is a network/communications Thread - Class within the program that handles all of the communications between the server and client.

    - There is a login screen that gathers the user's username and password as well as the location of the server (IP Addr.)

    - The login screen dispatches this data to the LoginProgress panel which contains the problematic progress bar, this class is responsible for starting up the Thread that handles communications and is supposed to report back to the user through the GUI about the current state of the login process.

    -> Now the problem is that I can't get this updater to work properly. Should I create another thread that handles the updating of the progress bar? Basically what I've got is this (and it's not working):

        public void start() {
            SwingUtilities.invokeLater(new Runnable() {
                @SuppressWarnings("static-access")
                public void run() {
                    // - Setup Communications
                    comms = new Comms(address, username, userhash, passhash);
                    comms.start();
                    // - Update Status Bar
                    while(comms.isConnected() == false)
                    {
                        setStatus(comms.currentStatus);
                        setPercent(comms.currentPercent);
                        try{
                            Thread.currentThread().sleep(100);
                        }catch(Exception e){
                            //Do Nothing
                        }
                        if(comms.getResult().startsWith("F"))
                        {
                            progressBar.setEnabled(false);
                            setStatus(comms.getResult().split("-")[1]);
                        }
                    }
                    // - Once Logged In
                    if(comms.getResult().startsWith("S"))
                    {
                        //Successful Login
                        setPercent(100);
                        setStatus("Login Successful!");
                        try{
                            Thread.currentThread().sleep(2000);
                        }catch(Exception e){
                            //Do Nothing
                        }
                        parent.setComms(comms);
                        parent.setVisible(true);
                        dispose();
                    }else{
                        LoginPanel lp = new LoginPanel(parent, (Config)UtilityFunctions.LoadObject("config.dbf"));
                        lp.centerFrame();
                        lp.setVisible(true);
                        dispose();
                    }
                }
            });
        }

    This is (what should be) the public static void main(String[] args) {...} function but because the function isn't static it cannot be such a functions which is why I changed it to public void start() {...}

    This isn't working. It just sits at 0% and doesn't do anything, it hangs. The cancel button that I've also included on the panel doesn't work, nor does the exit X.

    If anyone has any experience implementing progress bars or knows how to go about tackling this situation please let me know. I'd like to finally get down how to do progress bars.

    Thanks for the help!
    ... Ellipsis ...
    Java | C/C++ | x86 Assembly | Python
    HTML | PHP | CSS | Flash Actionscript


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Progress Bar Problem

    a) by 'progress bar' I assume you mean a JProgressBar? b) You aren't necessarily starting a new thread, but throwing all the work into the Swing EDT thread. Since the Swing EDT is responsible for GUI updates and interaction, if code clogs it up the GUI will lock until that code completes. So create a separate thread that runs the client connection, and send updates to the EDT using SwingUtilities.invokeLater based upon the progress. Alternatively use a SwingWorker implementation to do the longer task
    Last edited by copeg; October 22nd, 2010 at 02:30 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Location
    USA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Progress Bar Problem

    Yeah I meant JProgressBar, I should have been more clear on that one.

    The actual networking/connection (referred to in the code as "comms") is a separate thread. What I'm getting from what you said is that I can't put the progress bar updates in the Swing thread as it's clogging it up.

    I'll look into what you suggested, I've never worked closely with the Swing EDT thread itself so I will read up on that as well as look into this SwingWorker idea. If you have any good tutorials or suggested reading on this let me know.

    Thanks for your help I will post if I have any further problems with this issue.
    ... Ellipsis ...
    Java | C/C++ | x86 Assembly | Python
    HTML | PHP | CSS | Flash Actionscript

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Progress Bar Problem

    One of the more comprehensive articles on the subject
    Threads and Swing

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Progress Bar Problem

    Another good learning resource:
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Monitoring thread for progress bar.
    By relixus in forum Threads
    Replies: 1
    Last Post: October 4th, 2010, 12:08 AM
  2. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 07:01 AM
  3. [Swing-Progress Bar] Can't resize progress bar
    By Grabar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:27 PM
  4. progress bar problem
    By LeonLanford in forum AWT / Java Swing
    Replies: 4
    Last Post: October 24th, 2009, 07:14 AM
  5. Progress bar while uploading a file in web application
    By jazz2k8 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 8th, 2008, 07:32 AM