ContentPane and JProgressBar not showing

I created an extension JDialogthat has one component JProgressBar,, inside the content panel. JProgressBaris publicly available because I want the value to be set by the owner class. When I create a new dialog, the content panel does not appear at all, as a result of which everything that is behind it is 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 is ProgressBarDialogas 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();

Am I missing something because this (I thought) is a pretty simple implementation?

camickr SSCCE : TestDialog.java. , . , . , .

+3
4

a) . , setter.

b) JFrame . JDialog, .

c) JFrame JDialog - BorderLayout, reset .

d) setSize() pack() . setSize(). .

e) setSize() setPreferredSize() . , .

, . - , , . , , .

, Short, Self Contained, Compilable and Executable, Example Program (SSCCE), .

+2

preferredSize() size() . .

+1

BorderLayout:

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

, :

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);     
    }  
}

, , ProgressBar . , 0 ( ) .

, swing: http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

0

, z-

setVisible(true);
toFront();

opFront() .

0

Source: https://habr.com/ru/post/1714813/


All Articles