1

I already have a panel made (its a row of buttons), and have it located at the bottom of a frame (SOUTH), but I would like to add two rows (panels/ subpanels) beneath it (a text input line and output line if it matters). Right now the only thing I know to do is declare and add more panels, which would be fine, but when I specify .SOUTH they go over top of the previous panel.

EDIT: The solution I used

As suggested by Ted Hopp, I added my original panel (now called subPanel1), as well as the two new panels which were going on top of the original (subPanel2 & subPanel3), to a fourth "container panel" (bottomCotainerPanel). Since I only had three subPanels, this allowed me to specify where in the containerPanel each subPanel would go (using NORTH, CENTER, SOUTH, might have to do something slightly different if you had more than 3...), and then specify where the contianerPanel would go in the frame (SOUTH).

    subPanel1.setLayout(new GridLayout(1,6)); //Layout of subPanel1
subPanel1.add(clearButton);
subPanel1.add(customerNameLabel);
subPanel1.add(customerNameTextField);
subPanel1.add(showByNameButton);
subPanel1.add(openNewSavingsButton);
subPanel1.add(openNewCheckingButton);


subPanel2.add(sendChatTextField);
subPanel2.add(sendButton);
subPanel2.add(clearButton2);

subPanel3.add(receiveChatTextField);
subPanel3.add(nextButton);
subPanel3.add(previousButton);

bottomContainerPanel.setLayout(new GridLayout(3,1));   //Layout of Container Panel
bottomContainerPanel.add(subPanel1, BorderLayout.NORTH);
bottomContainerPanel.add(subPanel2, BorderLayout.CENTER);
bottomContainerPanel.add(subPanel3, BorderLayout.SOUTH);

tellerWindow.getContentPane().add(bottomContainerPanel, BorderLayout.SOUTH);
TotesNewb
  • 13
  • 1
  • 5

3 Answers3

4

You need to add a single container panel as the SOUTH panel of the frame. The container itself should have the layout that you want for everything that goes at the bottom.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

If you just want to split panel onto 2 equal parts at south and north use GridLayout. If you want something in the middle you can use BorderLayout.

If you want to give your user ability to change the sub-panels size use JSplitPane.

TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

I had a similar problem trying to put several rows of buttons into a panel borrowed from the ListDemo example. Well, the first thing to do is to read about BorderLayout: How to Use BorderLayout, or at least see the image shown there:
enter image description here

You cannot have multiple bottom rows in a BorderLayout. But you can use a nested layout. What we need is a BoxLayout, see How to Use BoxLayout:
enter image description here.

We just have to replace the buttons shown on the above image by rows of buttons.

public class MyStuff extends JPanel {
  ...
  public MyStuff() {
    super(new BorderLayout());
    ...
    JPanel buttonArea = new JPanel();
    buttonArea.setLayout(new BoxLayout(buttonArea, BoxLayout.PAGE_AXIS));
    add(buttonArea, BorderLayout.PAGE_END);
    ...
    //if you dislike the default center alignment:
    //panelWithButtons1.setAlignmentX(Component.LEFT_ALIGNMENT);
    buttonArea.add(...);// add the 1st panel with buttons
    buttonArea.add(...);// add the 2nd panel with buttons
    buttonArea.add(...);// add the 3rd panel with buttons
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127