Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put JButton in bottom right

I need to put a button in the bottom right of an otherwise empty JPanel

 +-----------------------------------+
 |                                   |
 |                                   |
 |                                   |
 |                                   |
 |                                   |
 |                                   |
 |                                   |
 |                                   |
 |                      +-----------+|
 |                      | Click Me! ||
 |                      +-----------+|
 +-----------------------------------+

How do I do that? It should be easy right? I would like to find the correct layout manager rather than using a sequence of nested panels.

JPanel panel = new JPanel();
panel.setLayout(new SomeKindOfLayoutManagerThatDoesThis());
panel.add(new JButton("Click Me!"), SETTINGS);
like image 665
Lucas Avatar asked Jun 23 '12 00:06

Lucas


2 Answers

I would suggest using the Border Layout manager with Flow Layout.

something like:

this.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton clickmeButton = new JButton("Click Me");
buttonPanel.add(clickmeButton);
this.add(buttonPanel,BorderLayout.SOUTH);
like image 97
henry bemis Avatar answered Oct 22 '22 06:10

henry bemis


You can use a combination of BoxLayout and size/alignment hints to achieve this.

like image 4
Jeffrey Avatar answered Oct 22 '22 06:10

Jeffrey