Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overlay a Jbutton over a JprogressBar

Tags:

java

swing

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

I'm trying unsuccesfully to obtain this:

enter image description here

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);
like image 481
Koop4 Avatar asked Jul 27 '15 14:07

Koop4


1 Answers

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);
    }
}
like image 111
m.cekiera Avatar answered Oct 18 '22 09:10

m.cekiera