0

I have a class that extends Jpanel and has an animation inside it. I have two action buttons that stop and start it however I need the buttons to appear on the bottom of the Jpanel. I have tried:

add(go,BOTTOM_ALIGNMENT);

or

add(go,BorderLayout.SOUTH);

and even flow layout,yet the buttons are always stuck to sit at the top of the jpanel. Any ideas? The buttons need to be created inside the Jpanel due to the action events.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Softey
  • 1,451
  • 3
  • 21
  • 42

1 Answers1

3

"however I need the buttons to appear on the bottom of the JPanel."

This add(go,BorderLayout.SOUTH); won't work, unless you set the layout of the JPanel to BorderLayout. It has a default FlowLayout that will leave the the button at the top.

setLayout(new BorderLayout());

Looking your previous question, you have a start button too. Not sure where you want to place that one. So here are option.

  • If you want both buttons at the bottom, wrap them in another JPanel and add that JPanel to the main JPanel using BorderLayout.SOUTH

  • If you want start at the top and go at the bottom, then BorderLayout.NORTH for start and BorderLayout.SOUTH for go

  • Which ever way you go, don't forget to set the layout of the class JPanel


EDIT

public class BallPanel extends JPanel {
    public BallPanel() {
        JPanel panel = new JPanel();
        JButton go = new JButton("GO");
        JButton stop = new JButton("STOP");
        panel.add(go);
        panel.add(stop);

        setLayout(new BorderLayout());
        add(panel, BorderLayout.SOUTH);
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you again for the quick answers. I had it manually set as flow layout and kept changing it incorrectly so never saw the results. Would I have to use flow layout to get the buttons to go side by side because the one button will take up the whole of the bottom? – Softey Mar 13 '14 at 13:22
  • You need to add both buttons to _another_ `JPanel`. And add _that_ `JPanel` to the `BorderLayout.SOUTH` of your class `JPanel`. You seemed to have it figured out in your first post I answered. I don't get what confuses you _this_ time around. – Paul Samsotha Mar 13 '14 at 13:27
  • The `buttonsPanel`, you don't need to set the layout. It has a default `FlowLayout` and will give you the side by side look you want. In case you didn't know, you can have more than one `JPanel`. It's common practice to nest a bunch of `JPanels` when needed. – Paul Samsotha Mar 13 '14 at 13:29
  • You can put another `JPanel` inside the class `JPanel`. I don't get what the problem is. See my **EDIT**. That is what I'm saying. Why can't you do that? I don't really understand the problem. – Paul Samsotha Mar 13 '14 at 13:36
  • Sorry I changed it now. I'm trying to do too many things at once and making silly mistakes all over the place. Focusing on this now and cleaned it all up and got it working. Thanks – Softey Mar 13 '14 at 13:45
  • +1 but i like to have a factory method outside the constructor , if you use in some other place go and stop buttons – nachokk Mar 13 '14 at 15:16