Question

My question is:
How to add a JPanel to a BorderLayout in the South - East?
This is my group of buttons:

buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridBagLayout());
buttonsPanel.add(cancel);
buttonsPanel.add(previous);
buttonsPanel.add(next);

And the cancel, previous and next buttons are JButtons-s, and buttonsPanel is a JPanel.
Declarations:

JPanel buttonsPanel;
JButton cancel;
JButton previous;
JButton next;

And my JFrame:
JFrame mainWindow; - the declaration.
The rest of code:

mainWindow.setJMenuBar(menubar);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.getContentPane().setLayout(new BorderLayout());
mainWindow.setSize(new Dimension(screenW / 3 + 200, screenH - 200));
mainWindow.setMinimumSize(new Dimension(screenW / 4, screenH - 500));
//mainWindow.getContentPane().add(BorderLayout.EAST, BorderLayout.SOUTH, buttonsPanel);
int w = mainWindow.getWidth();
int h = mainWindow.getHeight();
x = (screenW - w)/2;
y = (screenH - h)/2;
mainWindow.setLocation(x, y);
mainWindow.setVisible(true);

So what can I do to move the buttonsPanel in the South - East of the main window? Please help me.

Thanks in advance, Mateiaru

Was it helpful?

Solution

Create a JPanel with a BorderLayout, add this panel to SOUTH position of the main panel.

Create another JPanel, add your buttons to this panel (using what ever layout manager you need.

Now add the "button" panel to the south panel at the EAST position

OTHER TIPS

If I understand correctly, you would like to have your button panel at the south of your frame, and your buttons to be at the right of the button panel.

To do that, I would use a horizontal BoxLayout, containing a glue as its first element, to layout the buttons inside their button panel. Then I would add this button panel at the south of the content pane.

Read how to use those two layout managers in the swing tutorial, because you're confused about how a BorderLayour works: you can't add something at the south and at the east of such a layout.

Note that the tutorial page about the BoxLayout has an example that does exactly what you want.

buttonsPanel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top