-2

I am making a gui for a program and it uses JPanels to do so. I have 2 JPanels. The first one contains a JLabel, and JTextField. The second one contains a JLabel and JTextField. I need both of those at the bottom of the screen, one on top of the other. I tried BorderLayout.SOUTH but one of the Panels overwrites the other. And if I use BoxLayout.Y_AXIS then they are not centered or at the bottom. How would I do this?

EDIT: I fixed it. I forgot to set a border layout when I added the panel to the frame. So I fixed it.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

2

Solution: nest JPanels

Create a 3rd JPanel, one that uses BoxLayout oriented to BoxLayout.PAGE_AXIS, add your 2 JPanels to this, and then add this third JPanel to the main GUI (which hopefully uses BorderLayout) to the BorderLayout.PAGE_END position.


For example, my Minimal, Complete, and Verifiable Example Program:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

public class LayoutExample extends JPanel {
   public LayoutExample() {
      JPanel panel1 = new JPanel();
      panel1.add(new JLabel("Label 1"));
      panel1.add(new JTextField(10));
      panel1.setBorder(BorderFactory.createTitledBorder("Panel 1"));

      JPanel panel2 = new JPanel();
      panel2.add(new JLabel("Label 2"));
      panel2.add(new JTextField(10));
      panel2.setBorder(BorderFactory.createTitledBorder("Panel 2"));

      JPanel boxLayoutUsingPanel = new JPanel();
      boxLayoutUsingPanel.setLayout(new BoxLayout(boxLayoutUsingPanel, BoxLayout.PAGE_AXIS));
      boxLayoutUsingPanel.add(panel1);
      boxLayoutUsingPanel.add(panel2);
      boxLayoutUsingPanel.setBorder(BorderFactory.createTitledBorder("BoxLayout Panel"));

      setLayout(new BorderLayout());
      setBorder(BorderFactory.createTitledBorder("Main BorderLayout GUI"));
      add(Box.createRigidArea(new Dimension(400, 200)), BorderLayout.CENTER);
      add(boxLayoutUsingPanel, BorderLayout.PAGE_END);
   }

   private static void createAndShowGui() {
      LayoutExample mainPanel = new LayoutExample();

      JFrame frame = new JFrame("LayoutExample");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

If still stuck, create and post your Minimal, Complete, and Verifiable Example Program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • It partially worked. It made the text fields not full screen but they are still not at the bottom of the page. – Joel Butenhoff Mar 29 '15 at 16:56
  • @JoelButenhoff: see and run my example code above. If this doesn't fix it, then edit your question and post your own [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Mar 29 '15 at 17:01