• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing - Problem with JProgress indefenite state

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Elango Ramaraj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic