*
The moose likes Swing / AWT / SWT and the fly likes Swing - Problem with JProgress  indefenite state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing - Problem with JProgress  indefenite state" Watch "Swing - Problem with JProgress  indefenite state" New topic
Author

Swing - Problem with JProgress indefenite state

Elango Ramaraj
Greenhorn

Joined: May 18, 2007
Posts: 2
Hi,

I have problem with displaying JProgressBar. Application should show the indeterminate Progress indicator during the complex operations.

Attached Java Code
public class ProgressBarTest extends JFrame{

private JProgressBar progressBar = new JProgressBar(0, 100);
private JButton btnTest = new JButton("Test Indeterminate Progress Bar");

public ProgressBarTest(){
//initializes the components in the JFrame
initFrame();
}

private void initFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(progressBar, BorderLayout.NORTH);
add(btnTest, BorderLayout.SOUTH);

progressBar.setPreferredSize(new Dimension(200,25));

btnTest.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

boolean indeterminate = progressBar.isIndeterminate();

// starts progress indicator
progressBar.setIndeterminate(true);
progressBar.repaint();


///////////////////////////////////////////////////////////
// This is the place where complex operation takes place //
///////////////////////////////////////////////////////////

// This is a dummy operation.
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}

///////////////////////////////////////////////////////////
// Complex Operation Ends here. //
///////////////////////////////////////////////////////////

// stops progress indicator
progressBar.setIndeterminate(false);
}
});

pack();
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProgressBarTest().setVisible(true);
}
});
}
}

Thanks in Advance

Elango Ramaraj
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Elango Ramaraj
Greenhorn

Joined: May 18, 2007
Posts: 2
Hi Craig,

Thanks for immediate reply.

Most of the method calls that are mentioned in complex operation are not Thread Safe.
Can we have the progress bar animation in different Thread?

// starts progress indicator
progressBar.setIndeterminate(true);
//progressBar.repaint();

///////////////////////////////////////////////////////////
// This is the place where complex operation takes place //
///////////////////////////////////////////////////////////
// This is a dummy operation.
// Launch operation on a background thread.

Thread thread = new Thread(new Runnable() {
public void run() {
MOST OF THE OPERATIONS ARE NOT THREAD SAFE
}
});
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
///////////////////////////////////////////////////////////
// Complex Operation Ends here. //
///////////////////////////////////////////////////////////
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Most of the method calls that are mentioned in complex operation are not Thread Safe.
Can we have the progress bar animation in different Thread?

I'm not sure I understand. The new thread started for the complex task is not the event
dispatch thread. If the block includes methods that are not thread-safe that would have to
be dealt with in the block. Are you getting errors or weird behavior with this? What kind
of methods and/or difficulties are you dealing with?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing - Problem with JProgress indefenite state