JProgressBar doesn't fill completely on WindowsLookAndFeel

Did anyone experience the same problem? I don't understand if I am doing something incorrectly or there's some problem with painting, but JProgressBar on WindowsLookAndFeel, when I do a progressBar.setValue(to_the_max_value), it doesn't fill the entire progress bar with those green bars.

This doesn't happen for all the JProgressBars I create. Did anyone experience the same problem or I am doing something incorrectly.

The sample program, though not a great way to code, is able to reproduce the problem.

publicclass ProgressBarProblem{

publicstaticvoid main(String[] args)throws Exception{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

JPanel panel =new JPanel(new BorderLayout());

JProgressBar bar =new JProgressBar();

panel.add(bar, BorderLayout.CENTER);

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(panel);

frame.setSize(490, 50);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

bar.setMaximum(2);

bar.setValue(0);

Thread.sleep(1000);

bar.setValue(1);

Thread.sleep(1000);

bar.setValue(2);

}

}

If you can observe, you will notice the problem I am trying to explain. Setting the frame's width to 500 will work, but that's not the right way I guess.

Thank you!

[1957 byte] By [sri1025a] at [2007-10-3 0:35:33]
# 1
You'll need to repaint() the bar after adjusting it.
itchyscratchya at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
Sorry,But that doesn't work. I also have tried putting repaint() on every alternate line to just make sure. Can you tell me where exactly the repaint() must be done.Thank you!
sri1025a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
Well as soon as your code sets the value to 2 the application terminates. Put a repaint() and another sleep() in after that so you can actually see the result, and I'd have thought it should update ok.
itchyscratchya at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry again,The main thread terminates but not the AWTEventQueue, even after the last repaint() call, the GUI still stays! And the program stays alive!
sri1025a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 5

> You'll need to repaint() the bar after adjusting it.

Not really! The follwing works without using repaint().

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ProgressBarExample extends JPanel

{

public ProgressBarExample()

{

super(new BorderLayout());

final JProgressBar bar = new JProgressBar();

bar.setMaximum(2);

add(bar, BorderLayout.CENTER);

new javax.swing.Timer(1000, new ActionListener()

{

int value = 0;

public void actionPerformed(ActionEvent e)

{

if (value <= bar.getMaximum())

bar.setValue(value++);

}

}).start();

}

public static void main(String[] args)

{

JFrame frame = new JFrame("ProgressBarExample");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new ProgressBarExample());

frame.pack();

frame.setVisible(true);

}

}

I think the problem with the OPs code is that it is setting the value in the main thread and not the Swing event thread.

sabre150a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 6
even after the last repaint() call, the GUI still stays!Yes, of course, sorry. Brain's a bit addled today and I didn't read it properly!
itchyscratchya at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 7

If repaint() call not on EDT is the problem, then what's the problem with this modified code. Please don't do a pack() when you try to run, it may work properly. All I would like to know the reason for this problem. This is just a sample code which I wrote to explain a similar kind of problem, that's why I didn't put the ui updations on EDT.

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

public class ModifiedProgressBarProblem {

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

JPanel panel = new JPanel(new BorderLayout());

final JProgressBar bar = new JProgressBar();

panel.add(bar, BorderLayout.CENTER);

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(panel);

frame.setSize(490, 50);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

bar.setMaximum(2);

bar.setValue(0);

bar.repaint();

}

});

Thread.sleep(1000);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

bar.setValue(1);

bar.repaint();

}

});

Thread.sleep(1000);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

bar.setValue(2);

bar.repaint();

}

});

}

}

sri1025a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 8

import java.awt.*;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import javax.swing.SwingUtilities;

public class ModifiedProgressBarProblem

{

public static void main(String[] args) throws Exception

{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

JPanel panel = new JPanel(new BorderLayout());

final JProgressBar bar = new JProgressBar();

panel.add(bar, BorderLayout.CENTER);

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(panel);

frame.setSize(490, 50);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

bar.setMaximum(2);

bar.setValue(0);

new Thread()

{

public void run()

{

try

{

Thread.sleep(1000);

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

bar.setValue(1);

}

});

Thread.sleep(1000);

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

bar.setValue(2);

}

});

}

catch (InterruptedException e)

{

e.printStackTrace();

}

}

}.start();

}

}

sabre150a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 9
Sorry sabre150,I tried executing your code and it doesn't work :(
sri1025a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 10
> Sorry sabre150,> > I tried executing your code and it doesn't work :(Does it work if you don't change the L&F?
sabre150a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 11
I have this problem only with WindowsLookAndFeel, not with Java or JGoodies... It's because of the green bar filling thing on Windows...
sri1025a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...
# 12
> I have this problem only with WindowsLookAndFeel, not> with Java or JGoodies... It's because of the green> bar filling thing on Windows...OK, ignore my posts in this thread. Sorry to have wasted your time.
sabre150a at 2007-7-14 17:29:18 > top of Java-index,Desktop,Core GUI APIs...