Robdll
Robdll

Reputation: 6253

How to overlay a Jbutton over a JprogressBar

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

Upvotes: 5

Views: 827

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347334

As an alternative, you could simply add the 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 might be tempered to create a custom JProgressBar which could take something like a Action and which encompassed all this functionality automatically ;)

Upvotes: 4

m.cekiera
m.cekiera

Reputation: 5395

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

Upvotes: 8

Lukas Rotter
Lukas Rotter

Reputation: 4188

You can use GlassPane for this (Just saw your update, yours is also correct):

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

Upvotes: 4

Related Questions