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: JProgress Bar In GUI

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JProgress Bar In GUI

    Hi, I have created a GUI with a frame, panel, buttons etc etc

    I now need to add a JProgressBar. All the examples and tutorials I am finding are for creating a JProgressBar and addding it to the panel, I am happy with what my GUI looks like and dont want to mess around with the layout of it, so would prefer if the JProgressBar component just opens for the time it is needed in a new (almost window type of things) and then closes, is there a way I can do this. Or maybe i am getting confused, does a JProgressBar have to be visible (given a location on the panel) or does it just open like a pop up and then close once finished)??

    Sorry for such a long question, I am just confused about JProgressBar

    Thanks


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: JProgress Bar In GUI

    As far as I understand, JProgressBar is like other components and can be added and modified as such. If you want it to pop up in a new window, you may have to program a JDialog to do that.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JProgress Bar In GUI

    Thanks that makes sense, i've got the panel way to work, i might try the JDialog way later.
    Thanks =).

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JProgress Bar In GUI

    Hi, i am testing

    public class Test {
      public static displayPB(){
        JFrame parentFrame = new JFrame();
        parentFrame.setSize(500, 150);
        JLabel jl = new JLabel();
        jl.setText("Count : 0");
     
        parentFrame.add(BorderLayout.CENTER, jl);
        parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        parentFrame.setVisible(true);
     
        final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
        JProgressBar dpb = new JProgressBar(0, 500);
        dlg.add(BorderLayout.CENTER, dpb);
        dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dlg.setSize(300, 75);
        dlg.setLocationRelativeTo(parentFrame);
     
        Thread t = new Thread(new Runnable() {
          public void run() {
            dlg.setVisible(true);
          }
        });
        t.start();
        for (int i = 0; i <= 500; i++) {
          jl.setText("Count : " + i);
          dpb.setValue(i);
          if(dpb.getValue() == 500){
            dlg.setVisible(false);
            System.exit(0);
     
          }
          try {
            Thread.sleep(25);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        dlg.setVisible(true);
      }
    }

    when i call the method from another method such as
    	    public GUI() {     
    	    	displayPB();
    	    }
    	    }

    the frame and dialog and jprogress are displayed but the bar isnt shown and dosnt change.

    what am i doing wrong.


    Thanks

  5. #5
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: JProgress Bar In GUI

    Hi,

    The code to update the progress bar's value should be executed by a separate thread, otherwise the GUI will hang.