Question: I'm working on a swing GUI, and i'd like to overlay a button over a progressBar.
I have already wrote the code which update the progress bar and the button event, but i don't know how to manage the Layout!

currently the panel code is the following: 

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

        JFrame myFrame = new JFrame("myJfTitle");
        myFrame.setLayout(new BorderLayout());
        JPanel myPanel = new JPanel();
        JButton myButton = new JButton("Click me");
        JProgressBar myBar = new JProgressBar();
        myBar.setValue(50);
        myPanel.add(myButton);
        myPanel.add(myBar);
        myFrame.add(myPanel,BorderLayout.CENTER);
        myFrame.setVisible(true);
}

```

which gives the following result:

[![enter image description here](https://i.stack.imgur.com/OYCZ8.png)](https://i.stack.imgur.com/OYCZ8.png)

I'm trying unsuccesfully to obtain this: 

[![enter image description here](https://i.stack.imgur.com/rR5LG.png)](https://i.stack.imgur.com/rR5LG.png)

Can anyone explain me which kind of Layout (or whatever) should i use, or link me same reference from which i can read how to do it??

UPDATE:

by adding the following code, i managed to overly the 2 components, but i m still not able to enlarge the progress bar to fit the panel size:

```
LayoutManager overlay = new OverlayLayout(myPanel);
myPanel.setLayout(overlay);

```

Answer: 
Try with this:

```
public class Test {
    public static void main(String[] args) throws IOException {

        JFrame myFrame = new JFrame("myJfTitle");
        myFrame.setLayout(new BorderLayout());
        JButton myButton = new JButton("Click me");
        myButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        JProgressBar myBar = new JProgressBar();
        LayoutManager overlay = new OverlayLayout(myBar);
        myBar.setLayout(overlay);
        myBar.setValue(50);
        myBar.add(myButton);
        myFrame.add(myBar, BorderLayout.CENTER);
        myFrame.pack();
        myFrame.setSize(new Dimension(300,100));
        myFrame.setVisible(true);
    }
}

```
As an alternative, you could simply add the button to the `JProgressBar`, for example...

![ProgressBar](https://i.stack.imgur.com/6wJ1g.png)

```
JProgressBar pb = new JProgressBar() {

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        Dimension dim = getLayout().preferredLayoutSize(this);
        Insets insets = getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;
        size.width += insets.left + insets.right;
        size.height += insets.top + insets.bottom;
        return new Dimension(Math.max(size.width, dim.width), Math.max(size.height, dim.height));
    }

};
pb.setBorder(new EmptyBorder(4, 4, 4, 4));
pb.setLayout(new GridBagLayout());
JButton btn = new JButton("Go boom");
btn.setOpaque(false);
pb.add(btn);

pb.setValue(50);
add(pb);

```

Now, I might be tempered to create a custom `JProgressBar` which could take something like a `Action` and which encompassed all this functionality automatically ;)