Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in creating a progress bar 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I have a simple GUI which contains a progress bar.
The problem is that the progress bar is reaching to 100% and only than the Window loads. I would like that the progress bar will be working with the GUI and not after it. What do I need to change in the following code in order to acheive this:

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

public class Main extends JFrame
{
public Main()
{
getContentPane().add(new MyContainer());
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

public static void main(String[] args)
{
Main dasFenster = new Main();
dasFenster.setSize(600,200);
dasFenster.setTitle("Partitionsimage");
dasFenster.setVisible(true);
}
}

class MyContainer extends Container
{
public MyContainer()
{
setLayout(new BorderLayout(10,10));
setBackground(Color.darkGray);
NordPanel nord = new NordPanel();
add(nord, BorderLayout.NORTH );
SuedPanel sued =new SuedPanel();
add(sued, BorderLayout.SOUTH );
}
}

class NordPanel extends JPanel
{
private JTextField dasEingabeFeld = new JTextField(5);
private JButton derSicherungsButton = new JButton("Sichern");
private JButton derRestoreButton = new JButton("Restaurieren");
private JLabel dasStatusLabel = new JLabel("Keine Aktion");

public NordPanel()
{
setBackground(Color.green);
setLayout(new FlowLayout());
add(new JLabel("Partition # "));
add(dasEingabeFeld);
add(derSicherungsButton);
add(derRestoreButton);
add(new JLabel("Status: "));
add(dasStatusLabel);
derSicherungsButton.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int g = Integer.parseInt(dasEingabeFeld.getText());
}
});
}
}


class SuedPanel extends JPanel
{
public SuedPanel()
{
setBackground(Color.blue);
setLayout(new FlowLayout());
final int max = 10;
final JProgressBar balken = new JProgressBar( 0, max );
add( balken );

for ( int i = 1; i <= 10; i++ )
{
try
{
Thread.sleep( 1500 );
}
catch ( InterruptedException e ) { }

final int j = i;
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
balken.setValue( j );
balken.paint(balken.getGraphics());
balken.setStringPainted(true);
}
}
);
}
}

}
 
Doing a Thread.sleep like that is going to sleep the Swing Event Dispatch Thread! You're GUI will lock until the sleep ends.

Do the loop and sleep in a separate thread, but keep the bit the uses the SwingUtilities.invokeLater since it's crucial to cause GUI updates to occur on the Event Dispatch Thread.

Tim
 
Hey timw,

10X for the answer. Could you please elaborate this with a code example?
 
Not really. Bit too busy writing code for my boss. Just put the for loop in a thread of its own.

Tim
 
But the invokeLater uses the for loop i value and they will run in different threads...
 
You'll need to synchronize their access to this common variable, yes.

Tim
 
Thank you for the explanation timw.
Since timw is busy and I'm a bit new at Swing can someone please show me a correct code following timw's explanation?
 
Try this for starters.

Code:
class SuedPanel extends JPanel {
  public SuedPanel()
  {
    setBackground(Color.blue);
    setLayout(new FlowLayout());
    final int max = 10; 
    final JProgressBar balken = new JProgressBar( 0, max );
    add( balken );

    Runnable updateBar = new Runnable(){
      private Object lock = new Object();
      private int iProgress = 0;
      public void run(){
        
        for (int i = 1; i <= 10; i++) {
          try {
            Thread.sleep(1500);
          }
          catch (InterruptedException e) {}

          synchronized (lock){
            iProgress = i;
          }
          
          SwingUtilities.invokeLater( new Runnable() {
            public void run() {
              
              synchronized (lock){
                balken.setValue(iProgress);
              }

              balken.paint(balken.getGraphics());
              balken.setStringPainted(true);
            }
          }
          );
        }
      }
    };
    
    new Thread( updateBar ).start();
  }

}

BTW, LordOfTheRings. You've been using this forum long enough to know to post code using the code tags.

Tim
 
... so did this work for you LordOfTheRings?

Tim
 
Regarding the code tag: you are right, my apologies.
I am not infront my work computer at the moment (which has the environment), but I will tell you tomorrow if it is working once I'll test it. Meanwhile, thank you very much timw.
 
It is working, yet I need some time to learn what you have implemented there. Thank you very much Tim.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top