tags:

views:

379

answers:

4

I have created a JDialog extension that has one component, a JProgressBar, inside the content pane. The JProgressBar is public because I want the value to be set by the owner class. When I create a new Dialog, the content pane doesn't appear at all, resulting in whatever is behind it being displayed instead of the progress bar:

Since I'm not allowed to use img tags...

public class ProgressBarDialog extends JDialog {
    public JProgressBar bar;

    public ProgressBarDialog(Frame owner, String title) {
        super(owner, title);
        bar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
        bar.setValue(0);
        bar.setStringPainted(true);
        bar.setPreferredSize(new Dimension(200, 100));

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(bar, BorderLayout.CENTER);

        setSize(200, 100);
        setLocationRelativeTo(null);
        setVisible(true);
        toFront();
    }
    public void setProgress(int p) {
        bar.setValue(p);
    }
}

The usage code for this ProgressBarDialog is as follows

ProgressBarDialog progBarDialog = new ProgressBarDialog(null,"Submitting");
//Stuff gets done
progBarDialog.setProgress(20);
//Stuff gets done
progBarDialog.setProgress(45);
//Stuff gets done
progBarDialog.setProgress(70);
//Stuff gets done
progBarDialog.setProgress(100);
//Stuff gets done
progBarDialog.dispose();

Is there something I'm missing, because this is (I thought) a fairly simple implementation?

Upon suggestion of camickr, I created a test SSCCE here: TestDialog.java. As you can tell, the code is the exact same. The problem is that the test works and displays correctly. I have added all the code that is involved with the other implementation of the Dialog window.

+1  A: 

You should set the preferredSize() and the size() of the progress bar. That should make it appear.

jjnguy
I added these lines after setStringPainted(), but before adding it to the content pane: bar.setPreferredSize(new Dimension(200, 100)); bar.setSize(200, 100);and it still did not show.
Kevin Stich
A: 

You could use a BorderLayout:

contentPane.setLayout(new BorderLayout());
contentPane.add(bar, BorderLayout.CENTER);
frame.setSize(200,200);

Ok I did some test:

import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JProgressBar; 
public class FrameTest {    
    public static void main(String[] args) {     
        JFrame f = new JFrame();     
        JProgressBar b = new JProgressBar(0,100);
        b.getModel().setValue(50);   
        f.getContentPane().add(b);   
        f.setSize(new Dimension(200,200)); 
        f.setVisible(true);     
    }  
}

This paints the entire contents of the frame half filled, so it's not that the ProgressBar isn't visible. It's just that it fills the entire content which in combination with a 0 value in its model (no progress) just looks like that.

I'm sure you've read the always very valuable swing tutorial sections: http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

zedoo
getContentPane().setLayout(new BorderLayout()); getContentPane().add(bar, BorderLayout.CENTER);and a pack didn't make the bar appear.
Kevin Stich
A: 

My first guess would be to swap z-order after displaying

setVisible(true);
toFront();

in case the toFront() no-ops on a non-displayed widget.

Steve Gilham
I'm going to keep the calls in the order you presented, but still doesn't make the panel visible.
Kevin Stich
+2  A: 

a) The progress bar should not be public. If you want to change its value then create a setter method.

b) You should not be using a JFrame as a child window. You should be using a JDialog and then you specify the frame as the owner of the dialog.

c) The default layout of a JFrame or JDialog is a BorderLayout, so there is not need to reset the layout.

d) Using setSize() and pack() together is useless. The pack will override the setSize(). Use one or the other.

e) Using setSize() and setPreferredSize() on a component is useless. The layout manager will use the preferred size as a suggestion and then set the size based on its rules.

So from the few lines posted there are many problems. Although they are unlikely to cause any real problem, it makes me wonder what the rest of the program looks like. You might be leaving out valuable information that will help us solve the problem.

If you need further help then you need to create a Short, Self Contained, Compilable and Executable, Example Program (SSCCE), that demonstrates the incorrect behaviour.

camickr
You're right with d). But I'm not so sure about c): new JFrame().getContentPane.getLayout() returns a JRotePane$1.
zedoo
I've made some of the changes you have suggested, many of them were the way you suggested before hand but were stripped out in attempts to fix the issue. I made a SSCCE and posted it above.
Kevin Stich