Problem:
ClassA
static JProgressBar progressBar;
ClassB
ClassA.progressBar.setValue(0);
...
ClassA.progressBar.setValue(10);
...
ClassA.progressBar.setValue(20);
Progress bar displays 0, then 10 on top of the 0, then 20 on top of the 10.
Code:
public class ClassA
{
static JProgressBar progressBar;
public ClassA()
{
...
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
...
}
public static void main(String[] args)
{
ClassA cA = new ClassA();
cA.something();
}
public void something()
{
...
panel.add(progressBar, BorderLayout.CENTER);
...
}
...
}
public class ClassB
{
public void something()
{
...
ClassA.progressBar.setValue(0);
ClassA.progressBar.update(ClassA.progressBar.getGraphics());
...
}
...
}
I am also adding the progress bar to the panel. As it was mentioned above, it displays the progress bar and the percentage but it show each value on top of another.
EDIT: The problem is very similar to this: Java Swing revalidate() vs repaint(). However, instead of
the old content is still actually visible (though obscured by the the new content)
being problem with JPanel, mine is problem with JProgressBar.
I tried ClassA.progressBar.revalidate();
and ClassA.progressBar.repaint();
but nothing works except ClassA.progressBar.update(ClassA.progressBar.getGraphics());
, which, again, as it was mentioned above, displays new values on top of old values.