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:
I'm trying unsuccesfully to obtain this:
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);
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With