Results 1 to 11 of 11
  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default Load thread with JProgressBar

    Does someone know how I can load a thread with a JProgressBar?

    I mean, I've a simple thread.
    Java Code:
    class myLoadingThread extends Thread
    {
    public String[] s = new String[0];
     public myLoadingThread()
     {
     }
    
     public void run()
     {
      for(int i=0; i<=50000; i++)
      {
       String[] temp = new String[s.length+1];
       for(int j=0; j<i; j++)
       {
        temp[j] = s[j];
       }
       temp[i] = "test";
       s = temp;
      }
    }
    I wanna see on the JProgressBar at how many percent it is.

    Does someone know how I can do this?

    Thanks,

    Dennis

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Dennis View Post
    Does someone know how I can do this?
    You could use the SwingWorker class to execute your task.
    See: How to Use Progress Bars (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Or you need a reference to the progressbar and call setValue

  3. #3
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I've allready seen that article, but I need it for loading a thread.
    Not for a stringWorker.
    And the stringWorker doesn't load anything.

    I don't know how I can set it for a thread.
    Last edited by Dennis; 10-08-2010 at 06:34 PM.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    :mad: .......

    e.g.:
    Java Code:
    class myLoadingThread extends Thread {
    	public String[] s = new String[0];
    	private JProgressBar bar;
    
    	public myLoadingThread(JProgressBar bar) {
    		this.bar = bar;
    	}
    
    	public void run() {
    		for (int i = 0; i <= 50000; i++) {
    			String[] temp = new String[s.length + 1];
    			for (int j = 0; j < i; j++) {
    				temp[j] = s[j];
    			}
    			temp[i] = "test";
    			s = temp;
    			final int value = (int) (i / 50001d * 100);
    			SwingUtilities.invokeLater(new Runnable() {
    				public void run() {
    					bar.setValue(value);
    				}
    			});
    		}
    	}
    
    	public static void main(String[] args) throws Exception {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame frame = new JFrame();
    				JPanel panel = new JPanel();
    				JProgressBar bar = new JProgressBar(0, 100);
    				bar.setValue(0);
    				bar.setStringPainted(true);
    				panel.add(bar);
    				frame.add(panel);
    				frame.pack();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setLocationRelativeTo(null);
    				frame.setVisible(true);
    				new myLoadingThread(bar).start();
    			}
    		});
    	}
    }
    D

  5. #5
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    oh, you need to add the value settings INSIDE the runnable.

    Thank you! ;)

    ehm... but..

    when I have a thread without forloop?
    like:
    Java Code:
    public void run()
    {
     String s = "test1";
     String s2 = "test2";
     String s3 = "test3";
    }
    Than it's impossible to add a value on the progressBar.
    Last edited by Dennis; 10-08-2010 at 07:19 PM.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    ??
    Sorry I dont understand.
    What`s your question? :)

    If you only have one thread that has a short execution time like your example, it make no sense to display the progress or? But theoretically you could call the setValue method between the lines...but what are you expecting then? Sorry, don`t understand.

  7. #7
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Could you elaborate further want you want to happen in this thread?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  8. #8
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    Java Code:
    public void run()
    {
     String s = "test1";
     String s2 = "test2";
     String s3 = "test3";
    }
    is an example.

    But I wanna load hundred's of objects in a thread, but set between each two line the setValue command is much work.

    Does there is a function who cal the setValue command automatic at each 1% of the thread (by 1000 object, 1% = 10 objects)

  9. #9
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    I'm afraid you missed to place a Thread.sleep(long time) in the run().

    I just don't understand how your String[] s is implemented on the main().
    Plus, kindly check your code in regarding:
    Java Code:
    final int value = (int) (i / 50001d * 100);
    Is this correct, 50001d?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  10. #10
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    That works, but I think that's not the thing I need.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    1. See the publish and process methods of SwingWorker.
    2. Any change of state of Swing components should always be applied on the EDT and not from a background Thread. (see SwingUtilities#invokeLater / invokeAndWait)

    db

Similar Threads

  1. JprogressBar
    By cotarelo in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2010, 04:13 AM
  2. JProgressBar inside a JDialog
    By Mikey_S in forum AWT / Swing
    Replies: 10
    Last Post: 07-13-2009, 03:55 PM
  3. Updating a JProgressBar from another thread?
    By Xiphias in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 02:48 AM
  4. Placing a JProgressBar over my JPEG image
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 11:16 AM
  5. [SOLVED] Netbeans Desktop App &amp; JProgressBar
    By SebScoFr in forum NetBeans
    Replies: 3
    Last Post: 11-28-2008, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •