This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing - Problem with JProgress indefenite state

 
Elango Ramaraj
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Elango Ramaraj
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic