简体   繁体   中英

ContentPane and JProgressBar not visible

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:

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.

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM