How to overlay Jbutton on JprogressBar

I am working on a swing GUI and I would like to overlay a button on a progressBar. I already wrote code that updates the progress bar and button event, but I don’t know how to control the layout!

currently the panel code is as follows:

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 try to do this unsuccessfully:

enter image description here

Can someone explain to me what type of layout (or something else) I should use, or a link to my link from which I can read how to do it?

UPDATE:

adding the following code, I was able to override 2 components, but I still can’t increase the progress bar to fit the panel size:

 LayoutManager overlay = new OverlayLayout(myPanel); myPanel.setLayout(overlay); 
+5
source share
3 answers

Try the following:

 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); } } 
+8
source

You can use GlassPane to do this (just saw your update, yours is correct too):

 public class FTW { public static void main(String[] args) throws IOException { JFrame myFrame = new JFrame("myJfTitle"); myFrame.setSize(300,100); myFrame.setLayout(new BorderLayout()); JPanel myPanel = new JPanel(); JButton myButton = new JButton("Click me"); JProgressBar myBar = new JProgressBar(); myBar.setPreferredSize(new Dimension(myFrame.getWidth(),myFrame.getHeight())); //sets the size for the first time myFrame.addComponentListener(new ComponentAdapter() //sets the size everytime the frame is resized { public void componentResized(ComponentEvent evt) { Component c = (Component)evt.getSource(); myBar.setPreferredSize(new Dimension(myFrame.getWidth(),myFrame.getHeight())); } }); JPanel glass = (JPanel) myFrame.getGlassPane(); glass.setVisible(true); myBar.setValue(50); glass.add(myButton, BorderLayout.CENTER); myPanel.add(myBar); myFrame.add(myPanel); myFrame.revalidate(); myFrame.repaint(); myFrame.setVisible(true); } } 
+4
source

Alternatively, you can simply add a button to the JProgressBar , for example ...

Progressbar

 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 could be set up to create a custom JProgressBar that could take something like Action and which automatically included all these functions;)

+4
source

All Articles