JProgressBar doesnt show up
hi everybody!
i try do add a JProgressBar to a JPanel:
JProgressBar progressBar =new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Reading...");
progressBar.setBorder(border);
loadProgressPanel1.add( progressBar,BorderLayout.CENTER);
loadProgressPanel1.revalidate();
but it doesnt show up.. the panel is there, but empty.
where is my fault? :-(
how does the jprogressbar get its size?
thanks for helping me!
andreas
[719 byte] By [
anti43a] at [2007-11-27 8:45:28]

# 2
oh thank you. i know this document. and there
(in http://java.sun.com/docs/books/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java)
they add a progressbar to a panel:
JPanel panel = new JPanel();
panel.add(startButton);
panel.add(progressBar);
why does that not go for me, that was my question.
# 3
import javax.swing.*;
import java.awt.*;
public class ProgressBarDemo extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProgressBarDemo();
}
});
}
public ProgressBarDemo() {
Container content = getContentPane();
content.setLayout(new BorderLayout());
JProgressBar bar = new JProgressBar();
bar.setIndeterminate(true);
content.add(bar, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
Note that you probably really don't want to both setIndeterminate(true) and setStringPainted(true), since indeterminate means the string will always be 0.
If you're still having trouble, please post a complete example of isn't working.
# 4
By far the most common mistake using JProgressBar is to try to display it in the same thread as your long task.
This is Incorrect:
JButton long = new JButton("Long");
long.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
<Add progress bar to window or set it visible>
<do long task>
<remove progress bar from window>
}
The progress bar will never be displayed because the Event Thread never gets control back in order to process repainting events.
Correct version
long.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
<Add progress bar to window to set it visible>
Runnable r = new Runnable() {
public void run() {
<do long task>
}
};
new Thread(r).start();
}